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

时间:2018-05-10 15:58:57

标签: java multithreading exception state

我试图创建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--;
    }
}

提前致谢!

0 个答案:

没有答案