...
Code Block | ||
---|---|---|
| ||
class SocketReader implements Runnable { private final SocketChannel sc; private final Object lock = new Object; public SocketReader() throws IOException { this.sc = SocketChannel.open(new InetSocketAddress("somehost", 25)); } public void run() { ByteBuffer buf = ByteBuffer.allocate(1024); try { synchronized (this.lock) { while (!Thread.interrupted()) { this.sc.read(buf); // ... } } } catch (IOException ie) { // Forward to handler } } public static void main(String[] args) throws IOException, InterruptedException { SocketReader reader = new SocketReader(); Thread thread = new Thread(reader); thread.start(); Thread.sleep(1000); thread.interrupt(); } } |
...