![](/img/trans.png)
[英]How to make breakpoints in Runnable() break in Android studio?
[英]How to add breakpoints to Runnable classes
我的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");
}
}
}
}
旁注:控制台显示“循环”,所以我知道它被执行一次。 断点永远不会被击中,也不会执行多次。
我曾经遇到过我无法让 Intellij Idea 在断点处停止的情况。 基本上问题在于,一旦一个线程在断点处停止,其他线程就不会停止。
断点属性对话框中有一个设置可以防止这种情况发生。
右键单击断点并选择“查看断点”。
在对话框中选择一个断点。
您会注意到挂起复选框的右侧有 2 个单选按钮: All和Thread 。 全选。 另外,您可以将其设为默认值(右侧的“设为默认值”按钮)。 默认值将用于您添加的任何新断点。 旧的需要手动更改。
编辑Intellij 帮助站点上的附加信息: 断点选项
不要让异常悄悄溜走。 使用submit
方法返回的Future
。
Future<?> f=executorService.submit(backgroundImpositioner);
try {
f.get();
} catch(Exception ex) {
ex.printStackTrace();
}
然后你知道更多。
上面的代码只是为了找到你的实际问题。 对于生产环境,您不会等待完成,而是在异常发生时记录异常,例如:
executorService.execute(new FutureTask<Object>(backgroundImpositioner, null)
{
@Override
protected void done() {
if(!isCancelled()) try {
get();
} catch(InterruptedException ex) {
throw new AssertionError("on completed task", ex);
} catch(ExecutionException ex) {
logger.log(Level.SEVERE, "in background task", ex.getCause());
}
}
});
出于某种原因,我无法在while(true)
行中设置断点,但可以在run()
其他位置删除断点。
如果 run 方法中没有抛出异常,我只能假设其中一个调用永远不会返回。
你能在每次调用后放置输出语句来看看你能走多远吗? 我猜workstation.canAcceptWork()
或workstation.doWork()
是罪魁祸首。
我遇到了一个类似的问题,即 IntelliJ 从未在 Runnable 类的 run() 方法中命中我的断点。 我发现断点的唯一位置是在public void run() {
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.