使用线程池实现Server端,Socket编程?

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.ServerSocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Created by IntelliJ IDEA.
* User: fengjianhua
* Date: 11-5-28
* Time: 下午9:59
* To change this template use File | Settings | File Templates.
*/
public class Server {

    private class MyHandler implements Runnable {
        private Socket socket;

        public MyHandler(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {

        }
    }

    public static void main(String[] args) throws IOException {
        new Server().startup();
    }

    private void startup() throws IOException {
        ExecutorService es = Executors.newCachedThreadPool();
        try (
                ServerSocket ss = new ServerSocket(8888);
        ) {
            for (; ; ) {
                try {
                    es.submit(new MyHandler(ss.accept()));
                } catch (IOException e) {
                    es.shutdown();
                }
            }
        }
    }

}

你可能感兴趣的:(计算机语言,socket,server,import,class,file,string)