我while(true)
只运行一次,所以我试图添加断点以查看发生了什么,但它们似乎从未在我的run()
. 我正在使用 IntelliJ。在调试器中有一个“线程”选项卡。我是否需要在该选项卡中执行某些操作,例如选择正确的线程才能达到我的断点?我还看到了线程名称,并且想知道如何在此列表中找到正确的线程。
public class MyClass extends ServerWorkflowProcess<OtherClass> {
private ExecutorService executorService = Executors.newSingleThreadExecutor();
...
@Override
public void bootup() {
logger.info("Booting up: " + this);
BackgroundProcess backgroundImpositioner = new BackgroundProcess(this.getCollection());
executorService.submit(backgroundImpositioner);
}
@Override
public void shutdown() {
executorService.shutdown();
}
}
后台进程
public class BackgroundProcess implements Runnable {
protected volatile Logger logger = Logger.getLogger(BackgroundImpositioner.class.getName());
Collection<ImpositionWorkstation> impositionWorkstations;
public BackgroundImpositioner(Collection<ImpositionWorkstation> impositionWorkstation) {
this.impositionWorkstations = impositionWorkstation;
}
public void run() {
while(true) {
logger.info("looping");
for (ImpositionWorkstation workstation : impositionWorkstations) {
if (workstation.canAcceptWork()) {
//go do work in another thread so we're not blocking this
workstation.getWorkFromQueue();
try {
workstation.doWork();
} catch (ImpositionException e) {
logger.severe(e.getMessage());
}
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
logger.severe("Background impositioner was interrupted");
}
}
}
}
旁注:控制台显示“循环”,所以我知道它会执行一次。断点永远不会被击中,并且不会多次执行。