简体   繁体   English

创建的线程数超出了要求

[英]Creating more threads than requested

I am writing a multi threaded client program which listens for a connection which comes from the web browser.My problem is more threads are created than should be.For eg if i type in a url in the browser ,Just one thread should be created but in my case ,multiple threads are created. 我正在编写一个多线程的客户端程序,该程序侦听来自Web浏览器的连接。我的问题是创建了比应有的线程更多的线程。例如,如果我在浏览器中键入url,则仅应创建一个线程,但在我的情况下,创建了多个线程。

public void running() {

    try {
        for(;;){
        Socket socket=server.accept();
        Thread t= new Thread( new ClientHandler(socket));
        t.start();
         // calls the start method to start a thread which also starts the run method
        System.out.println("Thread id is " +t.getId() );}
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
             public static void main(String[] args) {

    Server server= new Server();
    server.running();
}
 }

The code presented will create one thread for each incoming connection. 呈现的代码将为每个传入连接创建一个线程。 No more. 不再。

If you are getting more threads than you are expecting, then the most obvious explanation is that the web browser is opening more connections to your server than you are expecting. 如果获得的线程数量超出预期,则最明显的解释是Web浏览器打开的服务器连接数量超出了预期。 For example, the browser may attempt to fetch a 'favico' for your "site" ... or send a HEAD before attempting a GET. 例如,浏览器可能会尝试为您的“站点”获取“ favico”……或在尝试GET之前发送HEAD。

Another possibility is that those extra threads that you are seeing are daemon threads created by the JVM; 另一种可能性是,您看到的那些额外线程是由JVM创建的守护程序线程。 eg the garbage collector thread, the finalizer thread and so on. 例如,垃圾收集器线程,终结器线程等。 Or maybe they never actually existed ... and you are misinterpreting something. 也许它们从未真正存在过……您正在误解某些东西。


The other thing to note is that it is a BAD IDEA to implement an HTTP service using plain TCP/IP sockets. 要注意的另一件事是,它是使用纯TCP / IP套接字实现HTTP服务的错误主意。 It is a lot of work to implement HTTP properly, and the chances are that you will get it a bit ... or a lot ... wrong, resulting in service that doesn't interoperate well with different browsers. 正确实现HTTP的工作量很大,而且很可能您可能会……或者是很多……错误,导致服务无法与不同的浏览器很好地互操作。 It is better to use an existing HTTP "stack"; 最好使用现有的HTTP“堆栈”。 eg an existing web application server / framework, or maybe the Apache HttpComponents stack. 例如现有的Web应用程序服务器/框架,或者Apache HttpComponents堆栈。

.For eg if i type in a url in the browser ,Just one thread should be created but in my case ,multiple threads are created. 例如,如果我在浏览器中输入url,则仅应创建一个线程,但就我而言,将创建多个线程。

Only if you assume the browser will create one connection. 仅当您假设浏览器将创建一个连接时。 You don't know that, and the evidence is against you. 您不知道,证据对您不利。 If the data returned by the URL contains relative URLs to images etc the browser will open additional connections to retrieve them concurrently. 如果URL返回的数据包含图像等的相对URL,则浏览器将打开其他连接以同时检索它们。

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

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