廖井涛
2024-03-04 eae17d27ec56a6b7887f5597335e38ca40273ef4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package builder;
 
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
import com.sun.net.httpserver.*;
 
public class ThreadHttpServer  {
    //Æô¶¯¶Ë¿Ú8080
    private static final int port=8880;
    private static final String Httpcontext="/demo";
    private static final int nThreads=8;
    public static void mains() {
        HttpServer httpServer;
        try {
            httpServer=HttpServer.create(new InetSocketAddress(port),0);
            httpServer.createContext(Httpcontext,new HttpHandlerDemo() );
//            ÉèÖò¢·¢Êý
            ExecutorService  executor=Executors.newFixedThreadPool(nThreads);
            httpServer.setExecutor(executor);
            httpServer.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
}