了解了一下Java實現(xiàn)簡單定時任務的三種方式,分別做了個例子。
自己寫篇以記錄、備忘。
注釋都清楚了,不另外解釋。
方式一:
- package test.timertask;
-
- /**
- * 使用Thread.sleep的方式來實現(xiàn)
- * 這種方式完全是自己實現(xiàn)的,該控制的地方控制,怎么寫都可以
- * @author wz
- */
- public class MyTimerTask {
- public static void main(String[] args) {
- MyTimerTask mt = new MyTimerTask();
- mt.task();
- }
-
- public void task(){
-
- final long timeInterval = 1000;
- (new Runnable() {
- public void run() {
- while (true) {
- // 執(zhí)行任務
- System.out.println("do the task……");
- try {
- Thread.sleep(timeInterval);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }).run();
- //這里直接調runnable的run方法了,這樣是由main方法的執(zhí)行線程直接轉去執(zhí)行這個runnable了(同一個線程)
- //當然也可new一個Thread,將runnable扔進去后調用其start方法(另啟一線程)
- }
- }
方式二:
- package test.timertask;
-
- import java.util.Timer;
- import java.util.TimerTask;
-
- /**
- *
- * @author wz
- * 通過Timer和TimerTask來實現(xiàn),jdk自己實現(xiàn)且封裝了其操作,
- * TimerTask實現(xiàn)Runnable接口,但是其實現(xiàn)正是留給用戶制定任務的接口
- * 然后通過Timer來執(zhí)行即可,Timer另啟線程來執(zhí)行任務,且實現(xiàn)了線程同步的,所示線程安全的
- * Timer維持一個任務隊列,可以調度多個任務
- *
- */
- public class MyTimerTask2 {
- public static void main(String[] args) {
- MyTimerTask2 mt2 =new MyTimerTask2();
- mt2.task();
- }
- public void task(){
- //制定一個任務
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
- System.out.println("執(zhí)行任務ing");
- }
- };
-
- // task - 所要安排的任務。
- // delay - 執(zhí)行任務前的延遲時間,單位是毫秒。
- // period - 執(zhí)行各后續(xù)任務之間的時間間隔,單位是毫秒。
- Timer timer = new Timer();
- long delay = 0;
- long intevalPeriod = 1 * 1000;
- timer.scheduleAtFixedRate(task, delay, intevalPeriod);
- }
- }
方式三:
- package test.timertask;
-
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.ScheduledFuture;
- import java.util.concurrent.TimeUnit;
-
- /**
- *
- * @author wz
- * jdk1.5提供了支持并發(fā)的定時任務處理工具ScheduledExecutorService
- * 1.以線程池的方式來執(zhí)行任務,效率高了
- * 2.可以設置開始延遲時間和任務取消時間
- *
- */
- public class MyTimerTask3 {
- public static void main(String[] args) {
- MyTimerTask3 mtt = new MyTimerTask3();
- mtt.taskForAPeriod();
- }
-
- private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
-
- public void taskForAPeriod() {
- //定義一個任務
- final Runnable task = new Runnable() {
- public void run() {
- System.out.println("執(zhí)行任務");
- }
- };
-
- // scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit):
- // 通過ScheduledExecutorService的scheduleAtFixedRate來執(zhí)行任務
- // 參數(shù)
- // command - 要執(zhí)行的任務
- // initialDelay - 首次執(zhí)行的延遲時間
- // period - 連續(xù)執(zhí)行之間的周期
- // unit - initialDelay 和 period 參數(shù)的時間單位
- final ScheduledFuture<?> taskHandle = scheduler.scheduleAtFixedRate(task, 1, 1, TimeUnit.SECONDS);
-
-
- // schedule(Runnable command,long delay,TimeUnit unit)創(chuàng)建并執(zhí)行在給定延遲后啟用的一次性操作。 (取消任務本身也是一個任務,一次去下就OK)
- // 參數(shù):
- // command - 要執(zhí)行的任務
- // delay - 從現(xiàn)在開始延遲執(zhí)行的時間
- // unit - 延遲參數(shù)的時間單位
- scheduler.schedule(new Runnable() {
- public void run() {
- taskHandle.cancel(true); //取消任務
- }
- }, 10, TimeUnit.SECONDS); //在10個TimeUnit.SECONDS時間單位后
- }
-
- }
|