private SelectorThread newThread() { SelectorThread st = new SelectorThread() { @Override public void listen() throws IOException, InstantiationException { initEndpoint(); final CountDownLatch latch = new CountDownLatch(1); controller.addStateListener( new ControllerStateListenerAdapter() { @Override public void onReady() { enableMonitoring(); latch.countDown(); } @Override public void onException(Throwable e) { if (latch.getCount() > 0) { logger().log(Level.SEVERE, "Exception during starting the controller", e); latch.countDown(); } else { logger().log(Level.SEVERE, "Exception during " + "controller processing", e); } } }); start(); try { latch.await(); } catch (InterruptedException ex) { } if (!controller.isStarted()) { throw new IllegalStateException("Controller is not started!"); } } }; st.setPort(PORT); st.setKeepAliveTimeoutInSeconds(10); return st; }