赞
踩
我并不喜欢干什么都动不动弄一大坨框架, 首先还是该掌握好自带的基础工具 , 然后是spring , 然后才是更大坨的工具
Java最基本的定时任务 : new一个线程并指定间隔和结束条件
public static boolean 是否循环 = true;
public static long 间隔时间毫秒 = 3000;
public static void 干点什么方法() { System.out.println("世界你好");}
public static void main(String...arguments) {
new Thread(()->{
while(是否循环) {
干点什么方法();
try{ Thread.sleep(间隔时间毫秒); }catch(Exception e) {System.err.println("sleep能有什么错");}
}
}).start();
}
long 三秒后=3000;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("要执行的代码");
timer.cancel();
}
}, 三秒后);
long 三秒后=3000 , 间隔1秒=1000;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
System.out.println("要执行的代码");
}
}, 三秒后, 间隔1秒);
long 三秒后=3000 , 间隔1秒=1000;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("要执行的代码");
try {Thread.sleep(500);}catch(Exception e) {} //不会多加500毫秒
}
}, 三秒后, 间隔1秒);
创建了一个任务队列 , 创建了一个执行线程
来自openJDK15的源码:
public class Timer {
/**
* The timer task queue. This data structure is shared with the timer
* thread. The timer produces tasks, via its various schedule calls,
* and the timer thread consumes, executing timer tasks as appropriate,
* and removing them from the queue when they're obsolete.
*/
private final TaskQueue queue = new TaskQueue();
/**
* The timer thread.
*/
private final TimerThread thread = new TimerThread(queue);
Timer会创建一个线程执行TaskQueue中的TimerTask
一个Timer一个线程 , 所以 , 如果想多个不相干的任务互不干扰,就多new几个Timer,分别放入
相干的任务可以放入同一个Timer
public Timer() {
this("Timer-" + serialNumber());
}
public Timer(String name) {
thread.setName(name);
thread.start();
}
public Timer(boolean isDaemon) {
this("Timer-" + serialNumber(), isDaemon);
}
public Timer(String name, boolean isDaemon) {
thread.setName(name);
thread.setDaemon(isDaemon);
thread.start();
}
isDaemon 表示 是否是守护线程 , 默认设置参考 Thread类
/* Whether or not the thread is a daemon thread. */
private boolean daemon = false;
不指定的话, 默认false 不是守护线程
public void schedule(TimerTask task, long delay, long period) {
if (delay < 0)
throw new IllegalArgumentException("Negative delay.");
if (period <= 0)
throw new IllegalArgumentException("Non-positive period.");
sched(task, System.currentTimeMillis()+delay, -period);
}
public void scheduleAtFixedRate(TimerTask task, long delay, long period) {
if (delay < 0)
throw new IllegalArgumentException("Negative delay.");
if (period <= 0)
throw new IllegalArgumentException("Non-positive period.");
sched(task, System.currentTimeMillis()+delay, period);
}
它们都调用sched方法
不同在于, 传入的 period 一个为负 , 一个为正
/** * Schedule the specified timer task for execution at the specified * time with the specified period, in milliseconds. If period is * positive, the task is scheduled for repeated execution; if period is * zero, the task is scheduled for one-time execution. Time is specified * in Date.getTime() format. This method checks timer state, task state, * and initial execution time, but not period. * Schedule(排程)指定的timerTask以指定的时间在指定的period(以毫秒为单位)执行。 如果period为正,则计划将任务安排为重复执行; 如果period为零,则task只执行一次。 时间以Date.getTime()格式指定。 此方法检查timer状态,task状态和初始执行时间,但不检查period(周期)。 */ private void sched(TimerTask task, long time, long period) { if (time < 0) throw new IllegalArgumentException("Illegal execution time."); // Constrain value of period sufficiently to prevent numeric // overflow while still being effectively infinitely large. if (Math.abs(period) > (Long.MAX_VALUE >> 1)) period >>= 1; synchronized(queue) { if (!thread.newTasksMayBeScheduled) throw new IllegalStateException("Timer already cancelled."); synchronized(task.lock) { if (task.state != TimerTask.VIRGIN) throw new IllegalStateException( "Task already scheduled or cancelled"); task.nextExecutionTime = time; task.period = period; task.state = TimerTask.SCHEDULED; } queue.add(task); if (queue.getMin() == task) queue.notify(); } }
sched源码
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。