`

(转)Java线程(五):Timer和TimerTask

阅读更多
<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>
分享到:
评论

相关推荐

    Java计时器Timer和TimerTask用法总结

    Java 计时器 Timer 和 TimerTask 用法总结 Java 中的计时器(Timer)和 TimerTask 是一种常用的延迟执行技术,用于实现延迟执行某个任务或事件。下面将详细介绍 Timer 和 TimerTask 的用法和实现原理。 Timer ...

    Java中的Timer和TimerTask简介(附完整代码实例)

    Java中的Timer和TimerTask简介(附完整代码实例)。 在Java中,Timer和TimerTask是两个经常用来实现定时器的类。这两个类使用起来非常方便,可以完成我们对定时器的绝大多数需求。 Timer是一种定时器工具,用来在一...

    Java中的Timer和TimerTask的使用.doc

    在Java编程语言中,`Timer`和`TimerTask`是两个关键的类,它们用于创建和管理定时任务。`Timer`类提供了一种机制来安排在未来某一特定时间或以固定间隔执行任务,而`TimerTask`是这些任务的实现基础。 `Timer`类是...

    JavaTimer和TimerTask实例教程Java开发

    在Java编程语言中,`Timer`和`TimerTask`是两个重要的类,它们用于调度周期性的任务执行。这两个类属于`java.util`包,提供了在后台线程中延迟执行任务或者定期执行任务的能力,这对于创建定时任务或者实现定时器...

    JAVA定时器之Timer+TimerTask

    在Java编程语言中,`Timer`和`TimerTask`是两个关键类,它们构成了Java的定时执行机制。这两个类在处理周期性任务或者延迟任务时非常有用,它们可以帮助开发者实现程序在特定时间点或者按照一定间隔执行特定操作的...

    详解JAVA Timer和TimerTask

    JAVA Timer和TimerTask是JAVA语言中两种重要的线程设施,用于实现定时执行任务。下面将详细介绍JAVA Timer和TimerTask的概念、用法和优点。 JAVA Timer JAVA Timer是一种线程设施,用于安排以后在后台线程中执行的...

    Java--Timer--TimerTask--.rar_java timer

    在Java编程语言中,`Timer`和`TimerTask`是两个关键类,它们用于调度周期性的任务执行。这两个类在多线程环境下尤其有用,能够帮助开发者安排在将来某一特定时间或定期执行的任务。让我们深入了解一下`Timer`和`...

    java类Timer和TimerTask的使用.pdf

    Java中的`Timer`和`TimerTask`类是用于创建和管理定时任务的工具,它们提供了在特定时间点或按照预设间隔执行任务的功能。这两个类是Java标准库`java.util`包的一部分,对于需要定时执行操作的应用场景非常有用。 `...

    Web中使用Timer和TimerTask定时获取数据。

    4. **替代方案**:考虑到`Timer`和`TimerTask`的一些限制,Java 5引入了`ScheduledExecutorService`,它提供了一套更强大且线程安全的定时任务调度接口。在新的项目中,通常推荐使用`ScheduledExecutorService`而...

    Timer与TimerTask(计时器)

    在Java编程语言中,`Timer`和`TimerTask`是用于执行定时任务的重要类。这两个类通常被用来处理那些需要在指定时间或周期性执行的任务,比如定时清理缓存、定时更新数据库等场景。 #### Timer简介 `Timer`类是一个...

    TimerTask与Timer设计时间监控

    在Java编程语言中,`TimerTask`和`Timer`是两个关键类,它们用于实现定时任务和调度。这两个类在处理需要定期执行的任务时非常有用,例如数据刷新、任务调度或者周期性的系统检查等。本篇文章将深入探讨`TimerTask`...

    解析Java中的Timer和TimerTask在Android中的用法和实例

    总结起来,Java的Timer和TimerTask在Android中是实现定时任务的强大工具,它们允许我们在后台线程中按照预设的时间间隔执行任务,并通过Handler与主线程通信,安全地更新UI。然而,使用时需要注意线程管理、资源释放...

    Java多线程机制(讲述java里面与多线程有关的函数)

    9.9 计时器线程Timer: Timer类和TimerTask类用于定时执行任务,可以设置间隔时间定期执行或一次性执行。 9.10 线程联合: 线程联合(Join)是让一个线程等待另一个线程完成后再继续执行,通过调用`join()`方法实现...

    Android Timer Task Demo

    这时,`TimerTask` 和 `Timer` 类就派上了用场。本篇将详细介绍如何在Android中使用`TimerTask`来实现定时任务,并通过一个简单的Demo进行示例说明。 `TimerTask`是Java提供的一个定时执行任务的类,它继承自`...

    使用Timer和TimerTask实现周期任务

    在这种情况下,`java.util.Timer` 和 `java.util.TimerTask` 类提供了非常有用的机制来实现周期性任务。本文将深入探讨这两个类的使用方法及其相关知识点。 首先,`Timer` 类是一个线程类,它负责调度任务的执行。...

    Timer,TimerTask 动态显示文字

    本篇将重点讲解如何利用`Timer`和`TimerTask`这两个工具来实现这样的功能,特别是针对`TextView`控件。 `Timer`类是Java.util包中的一个类,它提供了调度任务在未来某个时间点执行的能力。而`TimerTask`是`Timer`类...

    java.util.timer实现的简单定时任务

    在提供的`timerTask.txt`和`timerTask`文件中,可能包含了使用Timer和TimerTask实现的具体示例代码,你可以打开这些文件进一步学习和研究。通过实践和调试,你将更好地理解和掌握Java中的定时任务处理。

    几种定时任务(Timer、TimerTask、ScheduledFuture)的退出—结合真实案例【JAVA并发】.docx

    例如,在文件生成监控的场景下,我们可以创建一个内部类继承自TimerTask: ```java class FileStatusChecker extends TimerTask { private String fileName; // 构造器和业务逻辑 ... @Override public void ...

    ServletContextListener,Timer,TimerTask学习笔记

    然后,`Timer`和`TimerTask`是Java的定时器和定时任务类,它们用于在后台线程中定期执行任务。`Timer`对象负责调度任务,而`TimerTask`则表示具体的任务。创建一个`Timer`实例后,可以通过调用`schedule`或`...

    java_Timer_thread.rar_java thread timer_java timer_java 定时器_java

    总结,Java中的`Timer`和`TimerTask`是用于实现定时任务的基本工具,虽然它们在多线程编程中有一定的局限性,但在许多场景下仍然能够满足需求。对于更复杂的需求,可以考虑使用`ScheduledExecutorService`。了解并...

Global site tag (gtag.js) - Google Analytics