Code example for ExecutorService

Methods: shutdown, submit

0
            port = serverSocket.getLocalPort();
        } 
 
        public Future<byte[]> enqueue(final byte[] sendBytes, final int receiveByteCount)
                throws IOException { 
            return executor.submit(new Callable<byte[]>() {
                @Override public byte[] call() throws Exception { 
                    Socket socket = serverSocket.accept();
                    OutputStream out = socket.getOutputStream();
                    out.write(sendBytes);
 
                    InputStream in = socket.getInputStream();
                    byte[] result = new byte[receiveByteCount];
                    int total = 0;
                    while (total < receiveByteCount) {
                        total += in.read(result, total, result.length - total);
                    } 
                    socket.close();
                    return result;
                } 
            }); 
        } 
 
        public void shutdown() throws IOException { 
            serverSocket.close();
            executor.shutdown();
        } 
    } 
}