0
我需要中断swingworkers,但是如果线程正在运行某个片段,那么应该在此之后中断。事情是这样的:Java - 保护中断SwingWorker
public class worker extends SwingWorker<Integer, String>{
//(...) constructors and everything else
protected Integer doInBackground() throws Exception{
//Code that can be interrupted
while(true){
//(...) more code that can be interrupted
//This shouldn't be interrupted, has to wait till the loop ends
for(int i=0; i<10; i++){
//(...) more code that can be interrupted
}
}
}
中断工作者有:
Worker worker = new Worker();
worker.execute();
worker.cancel(true);
我已经试过同步块,但如果不工作,或者我只是在做它没有错肯定。
有没有办法?谢谢!
正如[本答案](https://stackoverflow.com/a/671053/4756299)中所述,您不会中断某个线程。以受控方式结束线程。 –