这边分别通过几种不同的方式,分别实现定时器任务的功能,分别是基于java.util.concurrent.ScheduledExecutorService、java.util.Timer及java.lang.Thread来实现及模拟实现,具体内容见实例代码
1. 基于ScheduledExecutorService方式 - 通过线程池可以获取不同的线程来执行任务单元,在复杂的任务分解单元,需要根据不同处理单元优化调度cpu资源时(复杂系统设计场景)
import java.util.concurrent.Executors;@b@import java.util.concurrent.ScheduledExecutorService;@b@import java.util.concurrent.TimeUnit;@b@@b@public class ScheduleTest {@b@ @b@ private static ScheduledExecutorService scheduleExecutorService=Executors.newScheduledThreadPool(4);@b@ private static long initialDelay4f = 200; // 初始延迟;毫秒为单位@b@ private static long delay4f = 1000; // 周期间隔;毫秒为单位@b@ @b@ public static void main(String[] args){@b@ @b@ Runnable worker=new Runnable() {@b@ @b@ @Override@b@ public void run() {@b@ System.out.println(Thread.currentThread().toString()+"@"+Thread.currentThread().hashCode());@b@ }@b@ };@b@ @b@ scheduleExecutorService.scheduleWithFixedDelay(worker, initialDelay4f, delay4f, TimeUnit.MILLISECONDS);@b@ scheduleExecutorService.scheduleWithFixedDelay(worker, initialDelay4f, delay4f, TimeUnit.MILLISECONDS);@b@ scheduleExecutorService.scheduleWithFixedDelay(worker, initialDelay4f, delay4f, TimeUnit.MILLISECONDS)@b@ scheduleExecutorService.scheduleWithFixedDelay(worker, initialDelay4f, delay4f, TimeUnit.MILLISECONDS);@b@ @b@ }@b@ @b@}
如上可以分别执行不同的定时单元控制台分别打印了四个线程调度执行结果
Thread[pool-1-thread-3,5,main]@1507737389@b@Thread[pool-1-thread-1,5,main]@536468534@b@Thread[pool-1-thread-2,5,main]@235262323@b@Thread[pool-1-thread-4,5,main]@1507737389@b@Thread[pool-1-thread-1,5,main]@536468534@b@Thread[pool-1-thread-3,5,main]@1507737389@b@Thread[pool-1-thread-4,5,main]@1507737389@b@Thread[pool-1-thread-2,5,main]@235262323
2. Timer&TimerTask - 只能调度一个线程完成任务,数据关系结构比较单一、简单
import java.util.Timer;@b@import java.util.TimerTask;@b@@b@public class TimerTest {@b@ @b@ private static Timer timer = new Timer(); @b@ private static long initialDelay4f = 200; // 初始延迟;毫秒为单位@b@ private static long delay4f = 1000; // 周期间隔;毫秒为单位@b@@b@ public static void main(String[] args) throws Exception{@b@ Runnable worker=new TimerTask() { @b@ public void run() { @b@ System.out.println(Thread.currentThread().toString()+"@"+Thread.currentThread().hashCode());@b@ } @b@ };@b@ timer.schedule((TimerTask)worker, initialDelay4f,delay4f);// 设定指定@b@@b@ }@b@@b@}
如两次会报错,执行结果如下所示
Thread[Timer-0,5,main]@1084284121@b@Thread[Timer-0,5,main]@1084284121@b@Thread[Timer-0,5,main]@1084284121@b@Thread[Timer-0,5,main]@1084284121
3. 通过Thread线程模拟Timer功能 - 最轻量级的定时器,方便设计开发,不建议直接用它做定时器
public class SingleThreadTimerTest{@b@ @b@ private static Thread timer=new Thread();@b@ private static long delay4f = 1000; // 周期间隔;毫秒为单位@b@@b@ public static void main(String[] args) throws Exception{@b@ @b@ Runnable worker=new Runnable() { @b@ public void run() { @b@ try {@b@ while(true){@b@ System.out.println(Thread.currentThread().toString()+"@"+Thread.currentThread().hashCode());@b@ Thread.sleep(delay4f);@b@ }@b@ } catch (InterruptedException e) {@b@ e.printStackTrace();@b@ }@b@ } @b@ };@b@ timer=new Thread(worker);@b@ timer.start();@b@ }@b@@b@}