做网站会后期维护吗建网站的流程及注意事项
web/
2025/9/26 17:33:22/
文章来源:
做网站会后期维护吗,建网站的流程及注意事项,做暖暖视频网站有哪些,网页版微信二维码一直失效转载自 ScheduledThreadPool中的Leader-Follow模式你知道不#xff1f;
ScheduledThreadPoolExecutor 是java中一个非常常用的定时调度的工具#xff0c;其提供了两种定时调度常用模式:
1.固定调度周期的任务执行。
2.固定延迟间隔的任务执行#xff0c;延迟间隔表示的是…转载自 ScheduledThreadPool中的Leader-Follow模式你知道不
ScheduledThreadPoolExecutor 是java中一个非常常用的定时调度的工具其提供了两种定时调度常用模式:
1.固定调度周期的任务执行。
2.固定延迟间隔的任务执行延迟间隔表示的是前一次执行完成到后一次执行开始的时间差。
1.scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit)
2.scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnit unit) ScheduledThreadPoolExecutor 是基于ThreadPoolExecutor实现的其继承了类ThreadPoolExecutor。从构造器可以看出为满足定时调度的需求其基于ThreadPoolExecutor定制了延迟工作队列DelayedWorkQueue。
public ScheduledThreadPoolExecutor(int corePoolSize) {super(corePoolSize, Integer.MAX_VALUE,DEFAULT_KEEPALIVE_MILLIS /*10L*/, MILLISECONDS,new DelayedWorkQueue());
}DelayedWorkQueue是一个基于最小堆的队列其在元素进出队列等BlockingQueue接口方法中使用可重入锁以保证线程安全。为了在O(1)的时间取消特定任务的调度ScheduleFutrueTask对象中还额外增加了heapIndex字段以记录其在最小堆中的位置。 Leader-Follower模式
定时调度线程池与一般线程池的一个重要不同:提交的任务是延迟而非立即执行的因此worker线程调用队列的take以取出执行任务必定是要阻塞的。考虑到等待延迟执行任务的线程无需都使用timed waiting等待队首任务一个task的执行线程只有一个全部唤醒只会造成大量无效的线程唤醒和阻塞操作。
ScheduledThreadPool采用了Leader-Follower模式等待第一个线程的任务也称为leader其调用available.awaitNanos待当前队列头部任务到达调度时间时唤醒。其余线程作为follower只需调用await方法无限阻塞等待直至被leader唤醒并重新完成抢锁-尝试执行队列首元素-抢leader-等待的循环。
public RunnableScheduledFuture? take() throws InterruptedException {final ReentrantLock lock this.lock;lock.lockInterruptibly();try {for (;;) {RunnableScheduledFuture? first queue[0];if (first null)available.await();else {long delay first.getDelay(NANOSECONDS);if (delay 0L)return finishPoll(first);first null; // dont retain ref while waitingif (leader ! null)available.await();else {Thread thisThread Thread.currentThread();leader thisThread;try {available.awaitNanos(delay);} finally {if (leader thisThread)leader null;}}}}} finally {if (leader null queue[0] ! null)available.signal();lock.unlock();}
}若新增元素位于堆顶此时需安排等待该元素定期调度或立即执行的leader线程。为此offer方法在新增元素应率先调度时清空leader并signal唤醒某个等待线程继续take方法中的循环:抢锁-尝试执行队首元素-抢leader-等待。
public boolean offer(Runnable x) {final ReentrantLock lock this.lock;lock.lock();try {/*insert into queue*/if (queue[0] e) {leader null;available.signal();}} finally {lock.unlock();}return true;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/81469.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!