[英]Java Thread.Join() method not waiting for thread to finish
I have this section of code: CustomerClient is basically a class that extends Thread. 我有这段代码:CustomerClient本质上是扩展Thread的类。 I am trying to spawn multiple threads but only one at a time. 我正在尝试生成多个线程,但一次只能生成一个。 However Im getting an illegal thread state exception. 但是我得到了非法的线程状态异常。 What am I doing wrong? 我究竟做错了什么?
CustomerClient cusClient = new CustomerClient("B", server_IP_1,
server_Port_1, server_IP_2, server_Port_2);
while (true) {
System.out.println("new thread A");
cusClient.start();
cusClient.join();
}
A thread can be started only once. 一个线程只能启动一次。 So need to initialize the thread object everytime in the loop. 因此需要在循环中每次都初始化线程对象。
while (true) {
CustomerClient cusClient = new CustomerClient("B", server_IP_1,
server_Port_1, server_IP_2, server_Port_2);
System.out.println("new thread A");
cusClient.start();
cusClient.join();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.