SocketListener extends Thread { @Override public final void interrupt() { try { server.close(); } catch (IOException ignore) { } finally { super.interrupt(); } } SocketListener(
            ServerSocket server,
            final ExecutorService executor,
            final SocketTaskFactory taskFactory);  SocketListener(
            final int bindingPort,
            final ServerSocketFactory serverSocketFactory,
            final ExecutorService executor,
            final SocketTaskFactory taskFactory); @Override final void run(); @Override final void interrupt();  }
@Test public void testInterrupt() throws IOException, InterruptedException { ServerSocket dummy = new ServerSocket(0); int listenerPort = dummy.getLocalPort(); dummy.close(); SocketListener lt = new SocketListener(listenerPort, ServerSocketFactory.getDefault(), Executors.newCachedThreadPool(), new SocketTaskFactory() { public Runnable getTask(Socket s) { return null; } }); lt.start(); Thread.sleep(200); lt.interrupt(); lt.join(); Socket s = new Socket(); try { s.connect(new InetSocketAddress(listenerPort)); Assert.fail("Got connection, but listener should be terminated"); } catch (IOException e) { } }