为什么即使我打电话给disruptor.shutdown我的程序也不会停止

时间:2015-09-17 06:19:55

标签: java lmax

我一直在尝试使用LMAX distruptor来缓冲我的某个程序生成的内容,并将它们作为一批记录发布到另一个程序(我仍然无法完成消费者批处理部分)。但即使不使用记录的批处理,它仍然可以正常工作。但我的问题是,尽管我使用了呼叫

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

如其中一个例子所示,它不会停止执行程序。它甚至可以在这些方法下执行语句。当我打印

executorService.isShutdown();

它返回true。有人可以帮我这个......

修改

"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)

2 个答案:

答案 0 :(得分:1)

只有当所有线程(非守护程序线程)完成时,您的Java进程才会停止。 可能有些线程仍在运行,可能是锁定,也许是循环。

要查看哪些线程仍在运行,您可以使用jdk-tools:

使用jps获取运行Java进程的ID:

C:\DVE\jdk\jdk8u45x64\jdk1.8.0_45\bin>jps
4112 TestExMain

使用正确的程序ID,使用命令jstack

C:\DVE\jdk\jdk8u45x64\jdk1.8.0_45\bin>jstack 4112
2015-09-17 09:12:45
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode):

"Service Thread" #9 daemon prio=9 os_prio=0 tid=0x000000001d208800 nid=0x1b7c runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"main" #1 prio=5 os_prio=0 tid=0x0000000002260800 nid=0x1324 waiting on condition [0x000000000224f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at com.example.TestExMain.main(TestExMain.java:8)

例如,在这里,您将看到一个线程Service Thread,这是一个守护进程 - 这个线程不会阻止您的程序关闭。 Thread main不是守护程序线程 - 在停止之前,Java-Process将等待此线程完成。 对于每个线程,您将看到堆栈跟踪,线程在哪个位置 - 您可以找到可能使线程无法运行的代码。

你所拥有的特定线程以某种方式被锁定(我不是为什么,它可能是wait()调用,synchronize块或其他一些锁定机制。当你调用disruptor.shutdown()时该线程没有停止时,它可能是你使用的lib中的一个错误。

答案 1 :(得分:1)

一些帮助我的提示:

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

使用ThreadFactory(或番石榴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)
  5. 不耐烦的关机示例:

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

    3。使用关机挂钩

    即使调用System.exit(int),也会调用它们,但如果您的JVM被SIGKILL(或非POSIX平台上的等效文件)杀死,则会调用它们。

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