译:http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html
子类:ScheduledThreadPoolExecutor
方法:
1、schedule(Runnable command, long delay, TimeUnit unit)
作用:在delay延时后执行一次性command任务
参数:callable(要执行的任务),delay(延时的时长),unit(时长单位)
返回:ScheduledFuture待完成的任务,等任务完成后调用它的get()方法返回null值并抛异常
eg:
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 ScheduledExecutorTest implements Runnable {
private String jobName = "";
public ScheduledExecutorTest(String jobname) {
super();
this.jobName = jobname;
}
@Override
public void run() {
System.out.println("execute " + jobName);
}
public static void main(String[] args) {
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
ScheduledFuture<?> able=service.schedule(new ScheduledExecutorTest("test1"), 1, TimeUnit.SECONDS);
try {
System.out.println(able.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
结果:
execute test1
null
2、schedule(Callable<V> callable, long delay, TimeUnit unit)
作用:在delay延时后执行一次性callable任务
参数:callable(要执行的任务),delay(延时的时长),unit(时长单位)
3、scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
作用:任务第一次在初始化延时时间initialDelay后执行,然后在时间段initialDelay + period后执行,接着在时间段initialDelay + 2 * period后执行,依此类推。如果任务的任一执行遇到异常,后续执行就会取消。否则,任务将只能通过取消或执行程序终止而终止。如果这个任务的任一执行的时间比period长,则后续执行都会晚于实际的执行时间,但不会同时执行。
参数:callable(要执行的任务),delay(延时的时长),unit(时长单位)
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 ScheduledExecutorTest implements Runnable {
private String jobName = "";
public ScheduledExecutorTest(String jobname) {
super();
this.jobName = jobname;
}
@Override
public void run() {
System.out.println("execute " + jobName);
}
public static void main(String[] args) {
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
service.scheduleAtFixedRate(new ScheduledExecutorTest("test1"), 1, 2, TimeUnit.SECONDS);
service.scheduleWithFixedDelay(new ScheduledExecutorTest("test2"), 1, 1, TimeUnit.SECONDS);
/*ScheduledFuture<?> able=service.schedule(new ScheduledExecutorTest("test1"), 1, TimeUnit.SECONDS);
try {
System.out.println(able.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}*/
}
}
4、scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
作用:任务第一次在初始化延时时间initialDelay后执行,第一个任务执行后经过delay时间后执行,依此类推。如果任务的任一执行遇到异常,后续执行就会取消。否则,任务将只能通过取消或执行程序终止而终止。
参数:callable(要执行的任务),delay(延时的时长),unit(时长单位)
5、继承的父类ExecutorService方法有:awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated, shutdown, shutdownNow, submit, submit, submit
6、继承父类Executor方法execute