首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

施用定时任务的一个例子

2012-11-16 
使用定时任务的一个例子package com.bill99.testimport java.util.concurrent.Executorsimport java.uti

使用定时任务的一个例子

package com.bill99.test;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;public class ScheduledExecutorTest {//线程池能按时间计划来执行任务,允许用户设定计划执行任务的时间,int类型的参数是设定//线程池中线程的最小数目。当任务较多时,线程池可能会自动创建更多的工作线程来执行任务public ScheduledExecutorService scheduExec = Executors.newScheduledThreadPool(1);//启动计时器public void lanuchTimer(){Runnable task = new Runnable() {public void run() {throw new RuntimeException();}};scheduExec.scheduleWithFixedDelay(task, 1000*5, 1000*10, TimeUnit.MILLISECONDS);}//添加新任务public void addOneTask(){Runnable task = new Runnable() {public void run() {System.out.println("welcome to china");}};scheduExec.scheduleWithFixedDelay(task, 1000*1, 1000, TimeUnit.MILLISECONDS);}public static void main(String[] args) throws Exception {ScheduledExecutorTest test = new ScheduledExecutorTest();test.lanuchTimer();Thread.sleep(1000*5);//5秒钟之后添加新任务test.addOneTask();}}

热点排行