Reactor Scheduler在主线程完成后继续运行很长时间?如何处理?

时间:2017-12-20 21:41:37

标签: java project-reactor

我有一个关于如何在使用Reactor 3时清理Scheduler工作线程的问题

Flux.range(1, 10000)
.publishOn(Schedulers.newElastic("Y"))
.doOnComplete(() -> { 
    // WHAT should one do to ensure the worker threads are cleaned up
    logger.info("Shut down all Scheduler worker threads");
})
.subscribe(x -> logger.debug(x+ "**"));

我执行上述代码时看到的是,一旦主线程完成运行,工作线程仍然处于WAITING状态一段时间。

sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1081)
java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:748)

有没有办法控制这个?即他们可以被处置onComplete()吗?我试过Schedulers.shutdownNow()但它没有帮助。

另一方面,当我这样做时,我能够控制调度程序处理。 哪个是首选/倡导的方式?

reactor.core.scheduler.Scheduler s = Schedulers.newElastic("X");
        Flux.range(1, 10000)
        .concatWith(Flux.empty())
        .publishOn(s)
        .doOnComplete(() -> {           
            s.dispose();
            logger.info("Shut down all Scheduler worker threads");
        })
        .subscribe(x -> logger.debug(x+ "**"));

2 个答案:

答案 0 :(得分:4)

如果您使用Schedulers.new[Elastic|...],则如果要关闭结果Scheduler,则有责任跟踪生成的Schedulers.shutdownNow()Schedulers.elastic()只会在您不明确时关闭库使用的默认调度程序,例如new(注意没有doFinally前缀)。

运行所有操作后清理的最佳方法是使用onError。这将在 onComplete | cancel | s.dispose()事件之后异步执行清理回调。更好地确保它是链中的最后一个运算符,尽管它试图在所有情况下真正执行最后一个。

唯一需要注意的是,它与前面的运算符在同一个线程中运行,换句话说,你试图关闭的线程 ... doFinally在{{{ 1}}回调会在处理完任务队列后关闭执行程序,因此在这种情况下,在线程消失之前会有一点延迟。

这是一个转储线程信息的示例,切换到自定义弹性线程并在doFinally中关闭它(添加过滤器并实现以提供更短的日志,更好地查看事件如何发挥):

@Test
public void schedulerFinallyShutdown() throws InterruptedException {
    ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
    Logger logger = Loggers.getLogger("foo");
    CountDownLatch latch = new CountDownLatch(1);
    reactor.core.scheduler.Scheduler s = Schedulers.newElastic("X");
    Flux.range(1, 10000)
        .publishOn(s)
        .concatWith(Flux.<Integer>empty().doOnComplete(() -> {
            for (ThreadInfo ti : threadMxBean.dumpAllThreads(true, true)) {
                System.out.println("last element\t" + ti.getThreadName() + " " + ti.getThreadState());
            }
        }))
        .doFinally(sig -> {
            s.dispose();
            logger.info("Shut down all Scheduler worker threads");
            latch.countDown();
        })
        .filter(x -> x % 1000 == 0)
        .materialize()
        .subscribe(x -> logger.info(x+ "**"));

    latch.await();
    for (ThreadInfo ti : threadMxBean.dumpAllThreads(true, true)) {
        System.out.println("after cleanup\t" + ti.getThreadName() + " " + ti.getThreadState());
    }
} 

打印出来:

11:24:36.608 [X-2] INFO  foo - onNext(1000)**
11:24:36.611 [X-2] INFO  foo - onNext(2000)**
11:24:36.611 [X-2] INFO  foo - onNext(3000)**
11:24:36.612 [X-2] INFO  foo - onNext(4000)**
11:24:36.612 [X-2] INFO  foo - onNext(5000)**
11:24:36.612 [X-2] INFO  foo - onNext(6000)**
11:24:36.612 [X-2] INFO  foo - onNext(7000)**
11:24:36.613 [X-2] INFO  foo - onNext(8000)**
11:24:36.613 [X-2] INFO  foo - onNext(9000)**
11:24:36.613 [X-2] INFO  foo - onNext(10000)**
last element    X-2 RUNNABLE
last element    elastic-evictor-1 TIMED_WAITING
last element    Monitor Ctrl-Break RUNNABLE
last element    Signal Dispatcher RUNNABLE
last element    Finalizer WAITING
last element    Reference Handler WAITING
last element    main WAITING
11:24:36.626 [X-2] INFO  foo - onComplete()**
11:24:36.627 [X-2] INFO  foo - Shut down all Scheduler worker threads
after cleanup   Monitor Ctrl-Break RUNNABLE
after cleanup   Signal Dispatcher RUNNABLE
after cleanup   Finalizer WAITING
after cleanup   Reference Handler WAITING
after cleanup   main RUNNABLE

答案 1 :(得分:2)

您需要在flux上调用blockLast()方法以确保它已完成。 如果你在并行运行助焊剂或在主线程之外的另一个线程中运行助焊剂,这一点尤其重要。

注意:您需要在链上调用publishOn,请参阅the reference guide以了解为什么publishon的位置很重要。

reactor.core.scheduler.Scheduler s = Schedulers.newElastic("X");
Flux.range(1, 10000)
    .concatWith(Flux.empty())
    .publishOn(s)
    .doOnComplete(() -> {           
       logger.info("Shut down all Scheduler worker threads");
    }).blocklast();
s.dispose();