
JAVA schedule pool 理解
JAVA schedule implement
如下例:
public static void main(String[] args) { ScheduledThreadpoolExecutor pool = new ScheduledThreadPoolExecutor(1); //task1 pool.schedule(()->{ System.out.println(1); },10,TimeUnit.SECONDS); //task2 pool.schedule(()->{ System.out.println(1); },5,TimeUnit.SECONDS); //task3 pool.schedule(()->{ System.out.println(1); },2,TimeUnit.SECONDS); } 要点 1.task1,task2,task3被加入到java.util.concurrent.ScheduledThreadPoolExecutor.DelayedWorkQueue中,经过了时间排序,最先执行的放到最上面。 2.worker线程使用take() or poll() 从DelayedWorkQueue中取出第一个元素,如果第一个元素还没有到时间,就等待执行。 问题: 1.如果放入新task过程中时,同时worker从DelayedWorkQueue取出任务,怎么同步? caller放入新的task时,会使用ReentrantLock加锁,此时worker线程就会等待直到任务添加完毕并释放锁。 2.如果worker从DelayedWorkQueue取出任务过程中,同时新的task放入到DelayedWorkQueue中,怎么同步? 如果取出任务并且在等待中(worker线程),那么在放入新任务时(caller线程),caller唤醒worker的等待,然后caller插入新任务,worker重新获取最新任务。👁️ 阅读量:0
© 版权声明:本文《JAVA schedule pool 理解》内容均为本站精心整理或网友自愿分享,如需转载请注明原文出处:https://www.zastudy.cn/wen/1686477310a264008.html。