ScheduleExecutorService接口中有四个重要的方法,其中scheduleAtFixedRate和scheduleWithFixedDelay在实现定时程序时比较方便。
下面是该接口的原型定义
Java.util.concurrent.ScheduleExecutorService extends ExecutorService extends Executor
1.schedule
schedule(Runnable command, long delay, TimeUnit unit),schedule方法被用来延迟指定时间后执行某个指定任务。
参数介绍:
command:执行线程
delay:延迟时间
unit:计时单位
测试代码如下:
package Timer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorServiceTest {
public static void main(String[] args) {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(5);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("开始:" + sdf.format(new Date()));
schedule.schedule(new Runnable() {
@Override
public void run() {
System.out.println("测试schedule方法:"+sdf.format(new Date()));
}
},1, TimeUnit.SECONDS);
}
}
测试结果:
开始:2016-11-04 11:58:09
测试schedule方法:2016-11-04 11:58:10
2.scheduleWithFixedDelay
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,TimeUnit unit)
创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟,如果任务的执行时间超过了廷迟时间(delay),下一个任务则会在 (当前任务执行所需时间+delay)后执行。
参数介绍:
command:执行线程
initialDelay:初始化延时
delay:上一次终止和下一次执行开始之间的延迟时间
unit:计时单位
测试代码如下:
package Timer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorServiceTest {
public static void main(String[] args) {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(5);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("开始:" + sdf.format(new Date()));
schedule.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("测试scheduleWithFixedDelay方法:"+sdf.format(new Date()));
}
},1,2, TimeUnit.SECONDS);
}
}
测试结果:
开始:2016-11-04 12:03:17
测试scheduleWithFixedDelay方法:2016-11-04 12:03:21
测试scheduleWithFixedDelay方法:2016-11-04 12:03:26
测试scheduleWithFixedDelay方法:2016-11-04 12:03:31
测试scheduleWithFixedDelay方法:2016-11-04 12:03:36
测试scheduleWithFixedDelay方法:2016-11-04 12:03:41
3.scheduleAtFixedRate
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。
如果任务的执行时间小于period,将会按上述规律执行。否则,则会按任务的实际执行时间进行周期执行。
参数介绍:
command:执行线程
initialDelay:初始化延时
period:两次开始执行最小间隔时间
unit:计时单位
测试代码(任务的执行时间小于period):
package Timer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorServiceTest {
public static void main(String[] args) {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(5);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("开始:" + sdf.format(new Date()));
schedule.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("测试scheduleAtFixedRate方法:"+sdf.format(new Date()));
}
},1,5, TimeUnit.SECONDS);
}
}
测试结果:
开始:2016-11-04 12:25:28
测试scheduleAtFixedRate方法:2016-11-04 12:25:32
测试scheduleAtFixedRate方法:2016-11-04 12:25:37
测试scheduleAtFixedRate方法:2016-11-04 12:25:42
测试scheduleAtFixedRate方法:2016-11-04 12:25:47
从上面的测试结果可以看到如果任务的执行时间小于period,每次间隔period执行一次任务。
测试代码(任务的执行时间大于period):
package Timer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorServiceTest {
public static void main(String[] args) {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(5);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("开始:" + sdf.format(new Date()));
schedule.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("测试scheduleAtFixedRate方法:"+sdf.format(new Date()));
}
},1,2, TimeUnit.SECONDS);
}
}
测试结果:
开始:2016-11-04 12:23:39
测试scheduleAtFixedRate方法:2016-11-04 12:23:43
测试scheduleAtFixedRate方法:2016-11-04 12:23:46
测试scheduleAtFixedRate方法:2016-11-04 12:23:49
测试scheduleAtFixedRate方法:2016-11-04 12:23:52
从上面的测试结果可以看到如果任务的执行时间大于period,则会按任务的实际执行时间进行周期执行。