gpt4 book ai didi

java - 当计数为 0 时,线程在 CountDownLatch await() 上被阻塞

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:13:16 27 4
gpt4 key购买 nike

在分析其中一个生产环境的日志时,我在倒计时等待()中看到一个处于“W​​AITING”状态的线程

...sun.misc.Unsafe.park(Native Method)
...java.util.concurrent.locks.LockSupport.park(Unknown Source)
...java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(Unknown Source)
...java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(Unknown Source)
...java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(Unknown Source)
...java.util.concurrent.CountDownLatch.await(Unknown Source)

闩锁被初始化为 1 并且另一个线程确实在闩锁的同一实例上调用了 countDown() 方法,但主线程仍然在闩锁上被阻塞。这导致 jvm 无限期挂起。

即使闩锁计数达到零时也被阻止听起来不合理,我正在寻找有关进一步解决此问题的建议。

有任何想法吗?

注 - 使用的 jvm 版本如下

java版本“1.5.0_15”
Java(TM) 2 运行时环境,标准版(构建 1.5.0_15-b04)
Java HotSpot(TM) 客户端 VM(构建 1.5.0_15-b04,混合模式,共享)

更新 - 以下是我在上面谈论的线程的代码片段


private class MyRunnable implements Runnable, Thread.UncaughtExceptionHandler {

private AtomicBoolean shouldStop = new AtomicBoolean(false);
private CountDownLatch stopLatch = new CountDownLatch(1);
private Thread currentThread;

public void run() {

Thread.currentThread().setName("My Thread");
Thread.currentThread().setUncaughtExceptionHandler(this);
currentThread = Thread.currentThread();
if (currentThread.isInterrupted()) {
logger.debug("The pool thread had its interrupted stattus set. Clearing...");
Thread.interrupted();
logger.debug("The pool thread had its interrupted stattus set. Clearing...DONE");
}
try {
doBusinessLogic(shouldStop);
} catch (Exception e) {
logger.error("An exception was encountered in the thread", e);
} finally {
if (currentThread.isInterrupted()) {
logger.debug("Clearing interupted status for the thread and returning to pool...");
Thread.interrupted();
}
stopLatch.countDown();
logger.debug("Stopped task after counting down on the latch");
}
}

public void stopThread() {
shouldStop.set(true);
logger.debug("Stop flag was set to true.. waiting for thread method to return...");
try {
stopLatch.await();
logger.debug("Stop flag was set to true... task has finished. Returning.");
} catch (InterruptedException e) {
logger.error("Interrupted while awaiting thread stop event...", e);
}
}

public void uncaughtException(Thread t, Throwable e) {
logger.error("An uncaught exception occurred in the task thread ", e);
}


private void doBusinessLogic(AtomicBoolean shouldStop) {
long sleepPeriod = 11;
while (!shouldStop.get()) {
try {
Thread.sleep(sleepPeriod);
} catch (InterruptedException e) {
logger.debug("Thread was interrupted.Clearing interrupted status and proceeding", e);
if (Thread.currentThread().isInterrupted())
Thread.interrupted();
}
if (shouldStop.get()) {
logger.debug("Stop flag was set. Returning.");
return;
}
try {
logger.debug("Performing business logic...");
//.....
logger.debug("Performing business logic...DONE");
} catch (Throwable e) {
logger.error("An exception occurred", e);
}
if (shouldStop.get()) {
logger.debug("Stop flag was set. Returning.");
return;
}
}

}


}

这是我在日志中看到的
DEBUG [main Thread] - Stop flag was set to true.. waiting for thread method to return...
DEBUG [My Thread] - Stop flag was set. Returning.
DEBUG [My Thread] - Stopped task after counting down on the latch

永远不会打印latch.await() 之后的logger 语句,并且线程转储还表明主线程在latch 上被阻塞。

最佳答案

我本来想把它写成评论,但我的声誉还不允许评论,所以我会在这里提供我的小经验。

我遇到了同样的问题。我的 countDownLatch 是从同步方法访问的:删除同步解决了这个问题(当然我不得不调整方法的代码以应对没有同步的情况)。我不知道如何解释这种行为。

关于java - 当计数为 0 时,线程在 CountDownLatch await() 上被阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10810570/

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