gpt4 book ai didi

java - 为什么即使我调用 disruptor.shutdown 我的程序也没有停止

转载 作者:行者123 更新时间:2023-11-29 04:59:15 29 4
gpt4 key购买 nike

我一直在尝试使用 LMAX 干扰器来缓冲我的一个程序产生的内容,并将它们作为一批记录发布到另一个程序(我仍然无法完成消费者批处理部分)。但即使不使用记录的批处理,它也能正常工作。但我的问题是尽管我曾经调用过

`disruptor.shutdown()` and  `executorService.shutdownNow()`

正如其中一个示例中给出的那样,它不会停止执行程序。它甚至也在这些方法下面执行语句。当我打印

executorService.isShutdown();

它返回真。有人可以帮我解决这个问题吗...

编辑

"pool-1-thread-1" prio=10 tid=0x00007f57581b9800 nid=0x1bec waiting on condition [0x00007f573eb0d000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000d9110148> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
at com.lmax.disruptor.BlockingWaitStrategy.waitFor(BlockingWaitStrategy.java:45)
at com.lmax.disruptor.ProcessingSequenceBarrier.waitFor(ProcessingSequenceBarrier.java:55)
at com.lmax.disruptor.BatchEventProcessor.run(BatchEventProcessor.java:123)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

最佳答案

一些对我有帮助的提示:

1。在 ExecutorService 线程上设置守护进程标志

使用 ThreadFactory(或 Guava 的 ThreadFactoryBuilder)执行此操作。

例子:

final ThreadFactory threadFactory = 
new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
final ThreadFactory threadFactory = Executors.defaultThreadFactory();
final Thread thread = threadFactory.newThread(r);
thread.setDaemon(true);
return thread;
}
};
final ExecutorService executorService =
Executors.newFixedThreadPool(threadCount, threadFactory);

2。关机命令

  1. Disruptor.shutdown(long, TimeUnit)
  2. Disruptor.halt()
  3. ExecutorService.shutdown()
  4. ExecutorService.awaitTermination(long, TimeUnit)

不耐烦关机示例:

try {
disruptor.shutdown(0, TimeUnit.NANOSECONDS);
// if shutdown is successful:
// 1. exception is not thrown (obviously)
// 2. Disruptor.halt() is called automatically (less obvious)
}
catch (TimeoutException e) {
disruptor.halt();
}
executorService.shutdown();
executorService.awaitTermination(0, TimeUnit.NANOSECONDS);

3。使用关闭 Hook

即使在 System.exit(int) 被调用时也会被调用,但如果你的 JVM 被 SIGKILL 杀死(或非 POSIX 平台上的等价物)则不会.

Runtime.getRuntime()
.addShutdownHook(
new Thread(
() -> {
// shutdown here
}));

关于java - 为什么即使我调用 disruptor.shutdown 我的程序也没有停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32623714/

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