Java实现简单定时任务

了解了一下Java实现简单定时任务的三种方式,分别做了个例子。

自己写篇以记录、备忘。

注释都清楚了,不另外解释。

方式一:

package test.timertask;

/**
 * 使用Thread.sleep的方式来实现
 * 这种方式完全是自己实现的,该控制的地方控制,怎么写都可以
 * @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) {
					// 执行任务
					System.out.println("do the task……");
					try {
						Thread.sleep(timeInterval);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).run();
		//这里直接调runnable的run方法了,这样是由main方法的执行线程直接转去执行这个runnable了(同一个线程)
		//当然也可new一个Thread,将runnable扔进去后调用其start方法(另启一线程)
	}
}

方式二:

package test.timertask;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 
 * @author wz
 * 通过Timer和TimerTask来实现,jdk自己实现且封装了其操作,
 * TimerTask实现Runnable接口,但是其实现正是留给用户制定任务的接口
 * 然后通过Timer来执行即可,Timer另启线程来执行任务,且实现了线程同步的,所示线程安全的
 * 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("执行任务ing");
			}
		};
		
//		task - 所要安排的任务。
//		delay - 执行任务前的延迟时间,单位是毫秒。
//		period - 执行各后续任务之间的时间间隔,单位是毫秒。 
		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提供了支持并发的定时任务处理工具ScheduledExecutorService
 * 1.以线程池的方式来执行任务,效率高了
 * 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("执行任务");
			}
		};
		
//		scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit):
//		通过ScheduledExecutorService的scheduleAtFixedRate来执行任务
//		参数
//		command - 要执行的任务
//		initialDelay - 首次执行的延迟时间
//		period - 连续执行之间的周期
//		unit - initialDelay 和 period 参数的时间单位
		final ScheduledFuture<?> taskHandle = scheduler.scheduleAtFixedRate(task, 1, 1, TimeUnit.SECONDS);
		
		
//		schedule(Runnable command,long delay,TimeUnit unit)创建并执行在给定延迟后启用的一次性操作。 (取消任务本身也是一个任务,一次去下就OK)
//		参数:
//		command - 要执行的任务
//		delay - 从现在开始延迟执行的时间
//		unit - 延迟参数的时间单位 
		scheduler.schedule(new Runnable() {
			public void run() {
				taskHandle.cancel(true);	//取消任务
			}
		}, 10, TimeUnit.SECONDS);	//在10个TimeUnit.SECONDS时间单位后
	}

}



  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值