Quartz源码解析 ---- 触发器按时启动原理

论坛 期权论坛     
选择匿名的用户   2021-5-30 00:19   546   0
<span style="color:rgb(51,51,51); font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:24px; background-color:rgb(245,245,245)">1.不停的轮询(while),直到收到停止任务的要求</span>
<br style="color:rgb(51,51,51); font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:24px; background-color:rgb(245,245,245)">
<span style="color:rgb(51,51,51); font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:24px; background-color:rgb(245,245,245)">2.算出下次触发的时间,比如是 09:10:00 ,减去当前时间 09:00:00 ,也就是还有10分钟才会触发下次任务。那就wait  10分钟</span>
<br style="color:rgb(51,51,51); font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:24px; background-color:rgb(245,245,245)">
<span style="color:rgb(51,51,51); font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:24px; background-color:rgb(245,245,245)">p.s. 最近刚好在看quartz的源码,还是比较容易看懂,建议你也好好看看</span>
<br style="color:rgb(51,51,51); font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:24px; background-color:rgb(245,245,245)">
<p><span style="color:rgb(51,51,51); font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:24px; background-color:rgb(245,245,245)">QuartzSchedulerThread.class</span></p>
<p><span style="color:rgb(51,51,51); font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:24px; background-color:rgb(245,245,245)"></span></p>
<pre class="blockcode"><code class="language-java"> triggerTime &#61; triggers.get(0).getNextFireTime().getTime();
                        long timeUntilTrigger &#61; triggerTime - now;
                        while(timeUntilTrigger &gt; 2) {
                            synchronized (sigLock) {
                                if (halted.get()) {
                                    break;
                                }
                                if (!isCandidateNewTimeEarlierWithinReason(triggerTime, false)) {
                                    try {
                                        // we could have blocked a long while
                                        // on &#39;synchronize&#39;, so we must recompute
                                        now &#61; System.currentTimeMillis();
                                        timeUntilTrigger &#61; triggerTime - now;//下次触发时间-当前时间
                                        if(timeUntilTrigger &gt;&#61; 1)
                                            sigLock.wait(timeUntilTrigger);//这里是关键,等待时间为  下次触发时间-当前时间
                                    } catch (InterruptedException ignore) {
                                    }
                                }
                            }
                            if(releaseIfScheduleChangedSignificantly(triggers, triggerTime)) {
                                break;
                            }
                            now &#61; System.currentTimeMillis();
                            timeUntilTrigger &#61; triggerTime - now;
                        }</code></pre>
<br>
<br>
<p></p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(85,85,85); font-family:&#39;microsoft yahei&#39;; font-size:15px; line-height:35px"> <span style="font-family:&#39;Microsoft YaHei&#39;; font-size:18px">从线程开始, 我们来看下QuartzSchedulerThread类(负责执行触发的Trigger的工作) : </span></p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(85,85,85); font-family:&#39;microsoft yahei&#39;; font-size:15px; line-height:35px"> </p>
<div class="dp-highlighter bg_java" style="font-family:Consolas,&#39;Courier New&#39;,Courier,mono,serif; width:938.516px; overflow:hidden; padding-top:1px; position:relative; border-color:rgb(204,204,204); color:rgb(85,85,85); line-height:35px; margin:18px 0px!important">
<div class="bar" style="padding-left:45px">
  <div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108)">
   <strong>[java]</strong>
   <a class="ViewSource" href="http://blog.csdn.net/wenniuwuren/article/details/42082981/#" rel="noopener noreferrer" style="text-decoration:none; border:none; padding:0px; margin:0px 10px 0px 0px; font-size:9px; color:rgb(12,137,207); background-color:inherit" target="_blank" title="view plain">view plain</a>
    <a class="CopyToClipboard" href="http://blog.csdn.net/wenniuwuren/article/details/42082981/#" rel="noopener noreferrer" style="text-decoration:none; border:none; padding:0px; margin:0px 10px 0px 0px; font-size:9px; color:rgb(12,137,207); background-color:inherit" target="_blank" title="copy">copy</a>
   <div style="position:absolute; left:503px; top:747px; width:29px; height:14px; z-index:99">
   </div>
    <a class="PrintSource" href="http://blog.csdn.net/wenniuwuren/article/details/42082981/#" rel="noopener noreferrer" style="text-decoration:none; border:none; padding:0px; margin:0px 10px 0px 0px; font-size:9px; color:rgb(12,137,207); background-color:inherit" target="_blank" title="print">print</a>
   <a class="About" href="http://blog.csdn.net/wenniuwuren/article/details/42082981
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP