ScheduledExecutorService从命名来看就是用来执行定时任务的。通常我们在执行定时任务的时候,主要是用来间隔执行和延迟执行,那么看看ScheduledExecutorService是如何定义的。
上原代码
package java.util.concurrent;
import java.util.concurrent.atomic.*;
import java.util.*;
public interface ScheduledExecutorService extends ExecutorService {
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit);
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit);
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);
}
ScheduledExecutorService接口继承了ExecutorService(点击查看源码)。所以ScheduledExecutorService也是一个可执行的服务。一共只有4个接口
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit);
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit);
参数:
Runnable,待执行的命令
delay, 延迟的时间
unit, 时间单位
返回:ScheduledFuture, 这个一个Future和Delayed的继承
解读:这个接口主要是用来延迟执行的,命令+延迟时间,还是很好理解的。
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit);
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit);
同上
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);
参数:
Runnable:是一个可执行的任务
initialDelay:初始延迟
period:时间间隔,这里的间隔指第一次成功执行到第二次成功执行的时间间隔,包括命令运行的时间
unit:时间单位,同时作用在initalDelay和period.
返回:ScheduledFuture, 这个一个Future和Delayed的继承
注意:一个是时间间隔,包括了命令运行的时间,如果想中止,则直接cancel ScheduledFuture即可
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);
参数:
Runnable:是一个可执行的任务
initialDelay:初始延迟
delay:时间间隔,这里的间隔指第一次成功执行完到第二次成功执行的时间延迟,包括不命令运行的时间
unit:时间单位,同时作用在initalDelay和delay.
返回:ScheduledFuture, 这个一个Future和Delayed的继承
注意:一个是时间间隔,不包括命令运行的时间,如果想中止,则直接cancel ScheduledFuture即可
具体的区别,还是写个例子来体会一下
package com.victor.test;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class ScheduledTest {
public static final long miltime = System.currentTimeMillis();
public static void main(String[] args) throws InterruptedException, ExecutionException {
fixRate();
fixDelay();
}
private static void fixRate() throws InterruptedException, ExecutionException{
final ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
Runnable task1 = new Runnable(){
public void run(){
System.out.println(getTimelasted(miltime)+": task1 running");
sleep();
}
};
final ScheduledFuture<?> future1 = service.scheduleAtFixedRate(task1, 1, 2, TimeUnit.SECONDS);
ScheduledFuture<String> future2 = service.schedule(new Callable<String>(){
public String call(){
try {
future1.cancel(true);
return "future1 cancelled";
} finally {
service.shutdown();
System.out.println("service closed");
}
}
}, 10, TimeUnit.SECONDS);
System.out.println(future2.get());
}
private static void fixDelay() throws InterruptedException, ExecutionException{
final ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
Runnable task1 = new Runnable(){
public void run(){
System.out.println(getTimelasted(miltime)+": task1 running");
sleep();
}
};
final ScheduledFuture<?> future3 = service.scheduleWithFixedDelay(task1, 1, 2, TimeUnit.SECONDS);
ScheduledFuture<String> future4 = service.schedule(new Callable<String>(){
public String call(){
try {
future3.cancel(true);
return "future3 cancelled";
} finally {
service.shutdown();
System.out.println("service closed");
}
}
}, 10, TimeUnit.SECONDS);
System.out.println(future4.get());
}
private static long getTimelasted(long miltime){
return (System.currentTimeMillis() - miltime);
}
private static void sleep(){
try {
Thread.currentThread();
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
执行结果
1014: task1 running
3014: task1 running
5014: task1 running
7013: task1 running
9013: task1 running
service closed
future1 cancelled
11024: task3 running
13034: task3 running
15044: task3 running
17054: task3 running
19065: task3 running
service closed
future3 cancelled