简体   繁体   English

在当前线程中启动java com.sun.net.httpserver.HttpServer侦听器,然后每个连接应具有一个线程

[英]Start java com.sun.net.httpserver.HttpServer listener in current thread, connections then should have one thread each

I am trying to make a simple HTTP server that streams data to users. 我正在尝试制作一个简单的HTTP服务器,将数据流传输给用户。 I would like the server listener to block main threads execution (take over the main thread): 我希望服务器侦听器阻止主线程执行(接管主线程):

public static void main(String [] args) throws Throwable {
    HttpServer server = HttpServer.create(new InetSocketAddress(80), 0);
    // The handler for connections
    server.createContext("/test", new MyHandler());
    // This is probably what needs tweaking
    server.setExecutor(null);
    // The start should block
    System.out.println("Server started at 0.0.0.0:80.");
    server.start();
    System.out.println("Server terminated without errors.");
}

The each connection will receive endless stream of data, so they need to be started in separate threads once they connect to the listening socket. 每个连接将接收无尽的数据流,因此一旦它们连接到侦听套接字,就需要在单独的线程中启动它们。

如果使用com.sun.net.httpserver的内部HttpServer包,则可以设置java.util.concurrent.Executor来确定如何将线程用于MyHandler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM