private void start ( boolean daemon ) { if ( daemon == Thread . currentThread ( ) . isDaemon ( ) ) { server . start ( ) ; } else { FutureTask < Void > startTask = new FutureTask < Void > ( new Runnable ( ) { @ Override public void run ( ) { server . start ( ) ; } } , null ) ; NamedDaemonThreadFactory . defaultThreadFactory ( daemon ) . newThread ( startTask ) . start ( ) ; try { startTask . get ( ) ; } catch ( ExecutionException e ) { throw new RuntimeException ( "Unexpected exception on starting HTTPSever" , e ) ; } catch ( InterruptedException e ) { // This is possible only if the current tread has been interrupted, // but in real use cases this should not happen. // In any case, there is nothing to do, except to propagate interrupted flag. Thread . currentThread ( ) . interrupt ( ) ; } } }
Start a HTTP server by making sure that its background thread inherit proper daemon flag .