gpt4 book ai didi

java - 为什么当我尝试访问同步列表时我的线程停止了?

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

出于某种原因,这个输出:

 public void msgNeedParts() {
// Blabla...
System.out.println(name + ": Try to print 'tasks'...");
synchronized(tasks) {
System.out.println(name + ": Tasks--" + tasks);
System.out.println(name + ": Did I manage to print it?");
tasks.add(new BinToDump(feeder, binNum));
}
stateChanged();
}

仅打印出“GantryAgent:尝试打印‘任务’...”,但不打印以下任何消息。我猜线程在尝试访问同步列表“任务”时不知何故“卡住了”,但我不知道为什么会这样。

“任务”是这样声明和初始化的:

private List<BinToDump> tasks = 
Collections.synchronizedList(new ArrayList<BinToDump>());

谁能指出我遗漏了什么?

啊!我怀疑我可能有罪魁祸首:

    /* If nothing left to do, return to original position. */

synchronized (tasks) {

if (tasks.isEmpty()) {

doReturnToOriginalPos();

}

}

在我的调度程序中(这是一个代理设计),我检查“任务”是否为空,然后调用 doReturnToOriginalPos()。也许这只是一遍又一遍地发生得太快以至于其他方法没有机会修改它?

确实是问题所在!它在我的调度程序中被调用得如此之快以至于没有其他东西可以访问“任务”。感谢大家的帮助!

最佳答案

某些东西锁定了任务。根据这是什么类型的应用程序,您应该能够获得系统的完整堆栈转储,但方法各不相同。例如,我认为大多数基于 Windows 的应用程序服务器上的 CTRL-Break 会执行此操作,我认为在 Linux 上发送 SIGQUIT 也会执行相同的操作。

获得堆栈转储后,您可以查看它以尝试找出哪个其他线程锁定了该对象。

您还可以使用 VisualVM为了相同的最终目标获取堆栈转储:

You can use Java VisualVM to take a thread dump (stack trace) while a local application is running. Taking a thread dump does not stop the application. When you print the thread dump you get a printout of the thread stack that includes thread states for the Java threads.

When you print a thread dump in Java VisualVM, the tool prints a stack trace of the active threads of the application. Using Java VisualVM to take a thread dump can be very convenient in cases where you do not have a command-line console for the application. You can use a stack trace to help diagnose a number of issues such as deadlocks or when an application hangs.

关于java - 为什么当我尝试访问同步列表时我的线程停止了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1824768/

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