- 浏览: 38639 次
- 性别:
- 来自: 北京
文章分类
最新评论
<div class="iteye-blog-content-contain" style="font-size: 14px;">
<p style="color: #333333; font-family: Arial; line-height: 26px;"> Timer和TimerTask可以做为实现线程的第三种方式,前两中方式分别是继承自Thread类和实现Runnable接口。</p>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> Timer是一种线程设施,用于安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行,可以看成一个定时器,可以调度TimerTask。TimerTask是一个抽象类,实现了Runnable接口,所以具备了多线程的能力。</p>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> 一个Timer可以调度任意多个TimerTask,它会将TimerTask存储在一个队列中,顺序调度,如果想两个TimerTask并发执行,则需要创建两个Timer。下面来看一个简单的例子: </p>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> </p>
<div class="dp-highlighter bg_java" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 700.90625px; overflow: auto; padding-top: 1px; color: #333333; line-height: 26px; 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; background-color: #f8f8f8; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[java]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/ghsau/article/details/7436458">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/ghsau/article/details/7436458">copy</a>
<div style="width: 18px; height: 18px;"> </div>
</div>
</div>
<ol class="dp-j" style="border: none; background-color: #ffffff; color: #5c5c5c; margin-bottom: 1px !important; margin-left: 45px !important;" start="1">
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> java.util.Timer; </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> java.util.TimerTask; </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> TimerTest { </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">static</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyTimerTask1 </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">extends</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> TimerTask { </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> run() { </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> System.out.println(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"爆炸!!!"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">static</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> main(String[] args) { </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> Timer timer = <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Timer(); </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> timer.schedule(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyTimerTask1(), </span><span class="number" style="margin: 0px; padding: 0px; border: none; color: #c00000; background-color: inherit;">2000</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);</span><span class="comment" style="margin: 0px; padding: 0px; border: none; color: #008200; background-color: inherit;">// 两秒后启动任务</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">} </span></li>
</ol>
</div>
<p><span style="color: #333333; font-family: Arial; line-height: 26px;"> schedule是Timer调度任务的方法,Timer重构了四个schedule方法,具体可以查看JDK API。</span></p>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> </p>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> 看一个稍复杂的例子,假设有这样一种需求,实现一个连环炸..,2秒后爆炸一次,3秒后爆炸一次,如此循环下去,这就需要创建两个任务,互相调度,代码如下:</p>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> </p>
<div class="dp-highlighter bg_java" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 700.90625px; overflow: auto; padding-top: 1px; color: #333333; line-height: 26px; 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; background-color: #f8f8f8; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[java]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/ghsau/article/details/7436458">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/ghsau/article/details/7436458">copy</a>
<div style="width: 18px; height: 18px;"> </div>
</div>
</div>
<ol class="dp-j" style="border: none; background-color: #ffffff; color: #5c5c5c; margin-bottom: 1px !important; margin-left: 45px !important;" start="1">
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> java.util.Date; </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> java.util.Timer; </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> java.util.TimerTask; </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> TimerTest { </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">static</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyTimerTask1 </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">extends</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> TimerTask { </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> run() { </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> System.out.println(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"爆炸!!!"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Timer().schedule(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyTimerTask2(), </span><span class="number" style="margin: 0px; padding: 0px; border: none; color: #c00000; background-color: inherit;">2000</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">static</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyTimerTask2 </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">extends</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> TimerTask { </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> run() { </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> System.out.println(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"爆炸!!!"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Timer().schedule(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyTimerTask1(), </span><span class="number" style="margin: 0px; padding: 0px; border: none; color: #c00000; background-color: inherit;">3000</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">static</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> main(String[] args) { </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> Timer timer = <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Timer(); </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> timer.schedule(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyTimerTask2(), </span><span class="number" style="margin: 0px; padding: 0px; border: none; color: #c00000; background-color: inherit;">2000</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">while</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">true</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">) { </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> System.out.println(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Date().getSeconds()); </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">try</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> { </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> Thread.sleep(<span class="number" style="margin: 0px; padding: 0px; border: none; color: #c00000; background-color: inherit;">1000</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">catch</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> (InterruptedException e) { </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="comment" style="margin: 0px; padding: 0px; border: none; color: #008200; background-color: inherit;">// TODO Auto-generated catch block</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> e.printStackTrace(); </span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">} </span></li>
</ol>
</div>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> 自JDK5之后,可以用ScheduledThreadPoolExecutor来替代Timer。</p>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> 本文来自:<span style="color: #336699;">高爽</span><span style="color: #336699;">|Coder</span>,原文地址:<span style="color: #336699;">http://blog.csdn.net/ghsau/article/details/7436458</span>,转载请注明。</p>
</div>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> Timer和TimerTask可以做为实现线程的第三种方式,前两中方式分别是继承自Thread类和实现Runnable接口。</p>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> Timer是一种线程设施,用于安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行,可以看成一个定时器,可以调度TimerTask。TimerTask是一个抽象类,实现了Runnable接口,所以具备了多线程的能力。</p>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> 一个Timer可以调度任意多个TimerTask,它会将TimerTask存储在一个队列中,顺序调度,如果想两个TimerTask并发执行,则需要创建两个Timer。下面来看一个简单的例子: </p>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> </p>
<div class="dp-highlighter bg_java" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 700.90625px; overflow: auto; padding-top: 1px; color: #333333; line-height: 26px; 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; background-color: #f8f8f8; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[java]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/ghsau/article/details/7436458">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/ghsau/article/details/7436458">copy</a>
<div style="width: 18px; height: 18px;"> </div>
</div>
</div>
<ol class="dp-j" style="border: none; background-color: #ffffff; color: #5c5c5c; margin-bottom: 1px !important; margin-left: 45px !important;" start="1">
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> java.util.Timer; </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> java.util.TimerTask; </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> TimerTest { </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">static</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyTimerTask1 </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">extends</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> TimerTask { </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> run() { </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> System.out.println(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"爆炸!!!"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">static</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> main(String[] args) { </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> Timer timer = <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Timer(); </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> timer.schedule(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyTimerTask1(), </span><span class="number" style="margin: 0px; padding: 0px; border: none; color: #c00000; background-color: inherit;">2000</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);</span><span class="comment" style="margin: 0px; padding: 0px; border: none; color: #008200; background-color: inherit;">// 两秒后启动任务</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">} </span></li>
</ol>
</div>
<p><span style="color: #333333; font-family: Arial; line-height: 26px;"> schedule是Timer调度任务的方法,Timer重构了四个schedule方法,具体可以查看JDK API。</span></p>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> </p>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> 看一个稍复杂的例子,假设有这样一种需求,实现一个连环炸..,2秒后爆炸一次,3秒后爆炸一次,如此循环下去,这就需要创建两个任务,互相调度,代码如下:</p>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> </p>
<div class="dp-highlighter bg_java" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 700.90625px; overflow: auto; padding-top: 1px; color: #333333; line-height: 26px; 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; background-color: #f8f8f8; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[java]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/ghsau/article/details/7436458">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/ghsau/article/details/7436458">copy</a>
<div style="width: 18px; height: 18px;"> </div>
</div>
</div>
<ol class="dp-j" style="border: none; background-color: #ffffff; color: #5c5c5c; margin-bottom: 1px !important; margin-left: 45px !important;" start="1">
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> java.util.Date; </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> java.util.Timer; </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> java.util.TimerTask; </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> TimerTest { </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">static</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyTimerTask1 </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">extends</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> TimerTask { </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> run() { </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> System.out.println(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"爆炸!!!"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Timer().schedule(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyTimerTask2(), </span><span class="number" style="margin: 0px; padding: 0px; border: none; color: #c00000; background-color: inherit;">2000</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">static</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyTimerTask2 </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">extends</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> TimerTask { </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> run() { </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> System.out.println(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"爆炸!!!"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Timer().schedule(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyTimerTask1(), </span><span class="number" style="margin: 0px; padding: 0px; border: none; color: #c00000; background-color: inherit;">3000</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">static</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> main(String[] args) { </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> Timer timer = <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Timer(); </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> timer.schedule(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyTimerTask2(), </span><span class="number" style="margin: 0px; padding: 0px; border: none; color: #c00000; background-color: inherit;">2000</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">while</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">true</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">) { </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> System.out.println(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Date().getSeconds()); </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">try</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> { </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> Thread.sleep(<span class="number" style="margin: 0px; padding: 0px; border: none; color: #c00000; background-color: inherit;">1000</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold;">catch</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> (InterruptedException e) { </span></span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="comment" style="margin: 0px; padding: 0px; border: none; color: #008200; background-color: inherit;">// TODO Auto-generated catch block</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> e.printStackTrace(); </span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li>
<li style="border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding-left: 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">} </span></li>
</ol>
</div>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> 自JDK5之后,可以用ScheduledThreadPoolExecutor来替代Timer。</p>
<p style="color: #333333; font-family: Arial; line-height: 26px;"> 本文来自:<span style="color: #336699;">高爽</span><span style="color: #336699;">|Coder</span>,原文地址:<span style="color: #336699;">http://blog.csdn.net/ghsau/article/details/7436458</span>,转载请注明。</p>
</div>
发表评论
-
(转)Java并发教程(Oracle官方资料)
2014-06-04 12:56 655本文是Oracle官方的Java并发相关的教程,感谢并发 ... -
(转)Java线程(篇外篇):线程本地变量ThreadLocal
2014-06-04 12:50 841首先说明ThreadLocal存放的值是线程内共享的,线程 ... -
(转)Java线程(篇外篇):阻塞队列BlockingQueue
2014-06-04 12:51 695好久没有写文章了,这段时间事情比较杂,工作也比较杂乱, ... -
(转)Java线程(九):Condition-线程通信更高效的方式
2014-06-03 18:17 755上一篇讲述了并发包下的Lock,Lock可以更好的 ... -
(转)Java线程(八):锁对象Lock-同步问题更完美的处理方式
2014-06-03 18:17 911Lock是java.util.concurrent.l ... -
(转)Java线程(七):Callable和Future
2014-06-03 18:16 656接着上一篇继续并发包的学习,本篇说明的是Calla ... -
(转)Java线程(六):线程池
2014-06-03 18:16 668自JDK5之后,Java推出了一个并发包,java.ut ... -
Java线程(四):线程中断、线程让步、线程睡眠、线程合并
2014-06-02 23:18 914最近在Review线程专栏,修改了诸多之前描述不够严谨的地 ... -
(转)Java线程(三):线程协作-生产者/消费者问题
2014-06-02 23:16 793上一篇讲述了线程的互 ... -
(转)Java线程(二):线程同步synchronized和volatile
2014-06-02 23:12 697上篇通过一个简单的例子说明了线程安全与不安全,在例子 ... -
(转)Java线程(一):线程安全与不安全
2014-06-02 23:09 573作为一个Java web开发 ...
相关推荐
Java 计时器 Timer 和 TimerTask 用法总结 Java 中的计时器(Timer)和 TimerTask 是一种常用的延迟执行技术,用于实现延迟执行某个任务或事件。下面将详细介绍 Timer 和 TimerTask 的用法和实现原理。 Timer ...
Java中的Timer和TimerTask简介(附完整代码实例)。 在Java中,Timer和TimerTask是两个经常用来实现定时器的类。这两个类使用起来非常方便,可以完成我们对定时器的绝大多数需求。 Timer是一种定时器工具,用来在一...
在Java编程语言中,`Timer`和`TimerTask`是两个关键的类,它们用于创建和管理定时任务。`Timer`类提供了一种机制来安排在未来某一特定时间或以固定间隔执行任务,而`TimerTask`是这些任务的实现基础。 `Timer`类是...
在Java编程语言中,`Timer`和`TimerTask`是两个重要的类,它们用于调度周期性的任务执行。这两个类属于`java.util`包,提供了在后台线程中延迟执行任务或者定期执行任务的能力,这对于创建定时任务或者实现定时器...
在Java编程语言中,`Timer`和`TimerTask`是两个关键类,它们构成了Java的定时执行机制。这两个类在处理周期性任务或者延迟任务时非常有用,它们可以帮助开发者实现程序在特定时间点或者按照一定间隔执行特定操作的...
JAVA Timer和TimerTask是JAVA语言中两种重要的线程设施,用于实现定时执行任务。下面将详细介绍JAVA Timer和TimerTask的概念、用法和优点。 JAVA Timer JAVA Timer是一种线程设施,用于安排以后在后台线程中执行的...
在Java编程语言中,`Timer`和`TimerTask`是两个关键类,它们用于调度周期性的任务执行。这两个类在多线程环境下尤其有用,能够帮助开发者安排在将来某一特定时间或定期执行的任务。让我们深入了解一下`Timer`和`...
Java中的`Timer`和`TimerTask`类是用于创建和管理定时任务的工具,它们提供了在特定时间点或按照预设间隔执行任务的功能。这两个类是Java标准库`java.util`包的一部分,对于需要定时执行操作的应用场景非常有用。 `...
4. **替代方案**:考虑到`Timer`和`TimerTask`的一些限制,Java 5引入了`ScheduledExecutorService`,它提供了一套更强大且线程安全的定时任务调度接口。在新的项目中,通常推荐使用`ScheduledExecutorService`而...
在Java编程语言中,`Timer`和`TimerTask`是用于执行定时任务的重要类。这两个类通常被用来处理那些需要在指定时间或周期性执行的任务,比如定时清理缓存、定时更新数据库等场景。 #### Timer简介 `Timer`类是一个...
在Java编程语言中,`TimerTask`和`Timer`是两个关键类,它们用于实现定时任务和调度。这两个类在处理需要定期执行的任务时非常有用,例如数据刷新、任务调度或者周期性的系统检查等。本篇文章将深入探讨`TimerTask`...
例如,在文件生成监控的场景下,我们可以创建一个内部类继承自TimerTask: ```java class FileStatusChecker extends TimerTask { private String fileName; // 构造器和业务逻辑 ... @Override public void ...
总结起来,Java的Timer和TimerTask在Android中是实现定时任务的强大工具,它们允许我们在后台线程中按照预设的时间间隔执行任务,并通过Handler与主线程通信,安全地更新UI。然而,使用时需要注意线程管理、资源释放...
9.9 计时器线程Timer: Timer类和TimerTask类用于定时执行任务,可以设置间隔时间定期执行或一次性执行。 9.10 线程联合: 线程联合(Join)是让一个线程等待另一个线程完成后再继续执行,通过调用`join()`方法实现...
这时,`TimerTask` 和 `Timer` 类就派上了用场。本篇将详细介绍如何在Android中使用`TimerTask`来实现定时任务,并通过一个简单的Demo进行示例说明。 `TimerTask`是Java提供的一个定时执行任务的类,它继承自`...
在这种情况下,`java.util.Timer` 和 `java.util.TimerTask` 类提供了非常有用的机制来实现周期性任务。本文将深入探讨这两个类的使用方法及其相关知识点。 首先,`Timer` 类是一个线程类,它负责调度任务的执行。...
本篇将重点讲解如何利用`Timer`和`TimerTask`这两个工具来实现这样的功能,特别是针对`TextView`控件。 `Timer`类是Java.util包中的一个类,它提供了调度任务在未来某个时间点执行的能力。而`TimerTask`是`Timer`类...
在提供的`timerTask.txt`和`timerTask`文件中,可能包含了使用Timer和TimerTask实现的具体示例代码,你可以打开这些文件进一步学习和研究。通过实践和调试,你将更好地理解和掌握Java中的定时任务处理。
然后,`Timer`和`TimerTask`是Java的定时器和定时任务类,它们用于在后台线程中定期执行任务。`Timer`对象负责调度任务,而`TimerTask`则表示具体的任务。创建一个`Timer`实例后,可以通过调用`schedule`或`...
总结,Java中的`Timer`和`TimerTask`是用于实现定时任务的基本工具,虽然它们在多线程编程中有一定的局限性,但在许多场景下仍然能够满足需求。对于更复杂的需求,可以考虑使用`ScheduledExecutorService`。了解并...