New file |
| | |
| | | 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(); |
| | | } |
| | | } |
| | | |
| | | } |