标签:延迟任务 周期性任务 timer scheduledthreadpoole
在实际应用中,有时候我们需要创建一些个延迟的、并具有周期性的任务,比如,我们希望当我们的程序启动后每隔1小时就去做一次日志记录。在JDK中提供了两种方法去创建延迟周期性任务。
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class TestTimer { public static void main(String[] args) { String time = new SimpleDateFormat("HH:mm:ss").format(new Date()); System.out.println("Start time : " + time); Timer timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { // TODO Auto-generated method stub String time = new SimpleDateFormat("HH:mm:ss").format(new Date()); System.out.println("Now Time : " + time); } }; //end task timer.schedule(task, 2000, 3000); } }
Start time : 21:36:08 Now Time : 21:36:10 Now Time : 21:36:13 Now Time : 21:36:16 Now Time : 21:36:19
import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class TestScheduledThreadPoolExecutor { public static void main(String[] args) { String time = new SimpleDateFormat("HH:mm:ss").format(new Date()); System.out.println("Start time : " + time); ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5); //创建5个执行线程 Runnable runnable = new Runnable() { @Override public void run() { // TODO Auto-generated method stub String time = new SimpleDateFormat("HH:mm:ss").format(new Date()); System.out.println("Now Time : " + time); } }; executor.scheduleWithFixedDelay(runnable, 2, 3, TimeUnit.SECONDS); } }
Start time : 22:12:25 Now Time : 22:12:27 Now Time : 22:12:30 Now Time : 22:12:33 Now Time : 22:12:36
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class SingleThreadTimer { public static void main(String[] args) { String time = new SimpleDateFormat("HH:mm:ss").format(new Date()); System.out.println("Start time : " + time); Timer timer = new Timer(); TimerTask task1 = new TimerTask() { @Override public void run() { // TODO Auto-generated method stub String time = new SimpleDateFormat("HH:mm:ss").format(new Date()); System.out.println("Task1 time : " + time); } }; TimerTask task2 = new TimerTask() { @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } String time = new SimpleDateFormat("HH:mm:ss").format(new Date()); System.out.println("task2 time : " + time); } }; timer.schedule(task1, 2000, 1000); timer.schedule(task2, 2000, 3000); } }
Start time : 22:22:37 Task1 time : 22:22:39 task2 time : 22:22:41 Task1 time : 22:22:41 Task1 time : 22:22:42 task2 time : 22:22:44 Task1 time : 22:22:44 Task1 time : 22:22:45 task2 time : 22:22:47 Task1 time : 22:22:47 Task1 time : 22:22:48
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class TestTimerTask { public static void main(String[] args) { System.out.println(new SimpleDateFormat("HH:mm:ss").format(new Date())); Timer timer = new Timer(); TimerTask task1 = new TimerTask() { @Override public void run() { System.out.println("1: " + new SimpleDateFormat("HH:mm:ss").format(new Date())); } }; TimerTask task2 = new TimerTask() { @Override public void run() { int[] arr = {1,2,3,4,5}; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } int index = (int)(Math.random()*100); System.out.println(arr[index]); System.out.println("2: " + new SimpleDateFormat("HH:mm:ss").format(new Date())); } }; timer.schedule(task1, 2000, 3000); timer.schedule(task2, 2000, 1000); } }
Timer与ScheduledThreadPoolExecutor
标签:延迟任务 周期性任务 timer scheduledthreadpoole
原文地址:http://blog.csdn.net/diaorenxiang/article/details/38827409