...
Code Block | ||
---|---|---|
| ||
class StopSocket implements Runnable { private finalvolatile Socket socket; public void run() { try { socket = new Socket("somehost", 25); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String string = null; while (!Thread.interrupted() && (string = br.readLine()) != null) { // Blocks until end of stream (null) } } catch (IOException ie) { // Forward to handler } } public static void main(String[] args) throws IOException, InterruptedException { StopSocket ss = new StopSocket(); Thread thread = new Thread(ss); thread.start(); Thread.sleep(1000); thread.interrupt(); } } |
...