- public class TimeServer {
- public static voidmain(String[] args)throws IOException {
- intport = 8080;
- ServerSocket server =null;
- try {
- server =new ServerSocket(port);
- System.out.println("The time server is start in port : " + port);
- Socket socket =null;
- TimeServerHandlerExecutePool singleExecutor =newTimeServerHandlerExecutePool(50, 10000);// 创建IO任务线程池
- while(true) {
- socket = server.accept();
- singleExecutor.execute(new TimeServerHandler(socket));
- }
- } finally {
- if(server !=null) {
- System.out.println("The time server close");
- server.close();
- server =null;
- }
- }
- }
- }
- public class TimeServerHandlerExecutePool {
- private ExecutorService executor;
- publicTimeServerHandlerExecutePool(intmaxPoolSize,int queueSize) {
- executor =new ThreadPoolExecutor(Runtime.getRuntime()
- .availableProcessors(), maxPoolSize, 120L, TimeUnit.SECONDS,
- newArrayBlockingQueue(queueSize));
- }
- public void execute(java.lang.Runnable task) {
- executor.execute(task);
- }
- }
来源: http://www.bubuko.com/infodetail-1979829.html