gpt4 book ai didi

java - 启动线程时出现非法线程状态异常

转载 作者:行者123 更新时间:2023-11-29 04:19:29 25 4
gpt4 key购买 nike

我试图创建 4 个线程并给它们分配任务。但是我不断收到 IllegalThreadStateException。我在这里隐藏了问题,但似乎没有人帮助我。下面是关键代码片段:

Sorter worker1 = new Sorter(theView,1);
Sorter worker2 = new Sorter(theView,2);
Sorter worker3 = new Sorter(theView,3);
Sorter worker4 = new Sorter(theView,4);

for(int nextTask=0 ; nextTask<List_of_FileContentsLists.size() ; nextTask++){
if(worker1.busy == false){
worker1.acquireTask(List_of_FileContentsLists.get(nextTask),fileList[nextTask]);
worker1.start();
}
else if(worker2.busy == false){
worker2.acquireTask(List_of_FileContentsLists.get(nextTask),fileList[nextTask]);
worker2.start();
}
else if(worker3.busy == false){
worker3.acquireTask(List_of_FileContentsLists.get(nextTask),fileList[nextTask]);
worker3.start();
}
else if(worker4.busy == false){
worker4.acquireTask(List_of_FileContentsLists.get(nextTask),fileList[nextTask]);
worker4.start();
}
else{
nextTask--;
}
}

提前致谢!

最佳答案

对于您启动一次的每个 worker,您不能像在循环中那样重复启动它。我希望 WorkerN 代表不同的线程。

当您尝试将线程的状态更改为不允许的状态时,会出现 IllegalThreadState。如需更多引用,请查看 javadoc:

https://docs.oracle.com/javase/7/docs/api/java/lang/IllegalThreadStateException.html

关于java - 启动线程时出现非法线程状态异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50276938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com