ScheduledExecutorServiceAPI计划任务使⽤使⽤callable进⾏延迟运⾏
public class MyCallableA implements Callable<String>{
@Override
public String call()throws Exception {
String name = Thread.currentThread().getName();
System.out.println(name+" callA start "+ System.currentTimeMillis());
TimeUnit.SECONDS.sleep(1);
System.out.println(name+" callA end "+ System.currentTimeMillis());
return"result callA";
}
}
public class MyCallableB implements Callable<String>{
@Override
public String call()throws Exception {
String name = Thread.currentThread().getName();
System.out.println(name+" callB start "+ System.currentTimeMillis());
System.out.println(name+" callB end "+ System.currentTimeMillis());鹅的部首
return"result callB";
}
}
public static void main(String[] args){
List<Callable> list =new ArrayList<Callable>();
list.add(new MyCallableA());
list.add(new MyCallableB());
// 创建单⼀任务执⾏计划
// ScheduledExecutorService ex = wSingleThreadScheduledExecutor();
// 创建多任务执⾏计划
ScheduledExecutorService ex = wScheduledThreadPool(2);
ScheduledFuture<String> fua = ex.(0),4, TimeUnit.SECONDS);
ScheduledFuture<String> fub = ex.(1),4, TimeUnit.SECONDS);
System.out.println("main x start "+System.currentTimeMillis());
try{
System.out.println("返回值A "+());
System.out.println("返回值B "+());
}catch(InterruptedException e){
// TODO Auto-generated catch block
e.printStackTrace();
}catch(ExecutionException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("main x end "+System.currentTimeMillis());
}
}
单⼀任务执⾏计划 测试结果
main x start 1601866894411
pool-1-thread-1 callA start 1601866898413
pool-1-thread-1 callA end 1601866899414
返回值A result callA
pool-1-thread-1 callB start 1601866899414
pool-1-thread-1 callB end 1601866899414
返回值B result callB
main x end 1601866899415
总的运⾏时间是延迟时间加上两个任务的运⾏时间,第⼆个⼈任务是在第⼀个任务执⾏完后再执⾏。
一周跑步几次合适创建多任务执⾏计划 测试结果
main x start 1601866979774
pool-1-thread-1 callA start 1601866983775
pool-1-thread-2 callB start 1601866983775
pool-1-thread-2 callB end 1601866983775
pool-1-thread-1 callA end 1601866984775
返回值A result callA
返回值B result callB
main x end 1601866984775
到达延迟执⾏时间后两个任务同时运⾏。
scheduleAtFixedRate 实现周期性执⾏
scheduleAtFixedRate(Runnable command,
孔子舞剧
long initialDelay,
long period,
TimeUnit unit);
command ⾃定义的runnable接⼝
initialDelay 延迟执⾏时间
period 周期
unit 时间单位
public class MyRunnableA implements Runnable {看朱成碧思纷纷
@Override
public void run(){
String name = Thread.currentThread().getName();
System.out.println(name+" callA start "+ System.currentTimeMillis());
try{
TimeUnit.SECONDS.sleep(2);
}catch(InterruptedException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(name+" callA end "+ System.currentTimeMillis());
}
}
public static void main(String[] args){
ScheduledExecutorService ex = wSingleThreadScheduledExecutor();
System.out.println("main x start "+System.currentTimeMillis());
ex.scheduleAtFixedRate(new MyRunnableA(),5,5, TimeUnit.SECONDS);
System.out.println("main x END "+System.currentTimeMillis());
爵士舞串词
}
运⾏时间⼤于周期时间(period ) 测试结果
main x start 1601867868366
main x END 1601867868369
pool-1-thread-1 callA start 1601867873370
pool-1-thread-1 callA end 1601867875371
pool-1-thread-1 callA start 1601867875372
pool-1-thread-1 callA end 1601867877375
pool-1-thread-1 callA start 1601867877377
延迟5秒后每两秒执⾏⼀次,⽽不是设定的1秒⼀次。
运⾏时间⼩于周期时间(period ) 测试结果
main x start 1601867934619
main x END 1601867934621
pool-1-thread-1 callA start 1601867939625
pool-1-thread-1 callA end 1601867941626
pool-1-thread-1 callA start 1601867944622
pool-1-thread-1 callA end 1601867946623
延迟5秒后每5秒执⾏⼀次,任务执⾏完后会等待到5秒后再开启下次任务的周期性执⾏。
scheduleWithFixedDelay 实现周期性运⾏
scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);
command ⾃定义的runnable接⼝
initialDelay 延迟执⾏时间
period 周期
unit 时间单位
设置多任务之间的固定间隔运⾏,不会存在超时与⾮超时情况,
ex.scheduleWithFixedDelay(new MyRunnableA(), 2, 3, TimeUnit.SECONDS);
表⽰延迟2秒运⾏,在任务A执⾏完毕后再间隔3秒开启下⼀周期任务执⾏。
getQueue()和 remove(Object x)⽅法
getQueue() 获取到队列中的任务,这些任务是将要运⾏的,正在运⾏的不在其中。
remove() 删除队列中的任务,正在运⾏中的任务⽆法删除,已运⾏完的任务是可以从队列中删除的。
public class MyRunnableB implements Runnable {
private String name;
public MyRunnableB(String name){
super();
this.name = name;
}
@Override
糯米肉丸子
public void run(){
System.out.println(name+" callA start "+ System.currentTimeMillis());
}
}
public static void main(String[] args){
ScheduledThreadPoolExecutor pool =new ScheduledThreadPoolExecutor(2);
Runnable a =new MyRunnableB("A");
Runnable b =new MyRunnableB("B");
Runnable c =new MyRunnableB("C");
ScheduledFuture aa= pool.scheduleAtFixedRate(a,3,1, TimeUnit.SECONDS);
ScheduledFuture bb=pool.scheduleAtFixedRate(b,5,1, TimeUnit.SECONDS);
ScheduledFuture cc=pool.scheduleAtFixedRate(c,10,1, TimeUnit.SECONDS);
System.out.println("***");
try{
//删除成功
//TimeUnit.SECONDS.sleep(4);
TimeUnit.SECONDS.sleep(6);
System.out.println("删除任务B "+ ve((Runnable)bb));
}catch(InterruptedException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
BlockingQueue<Runnable> queue = Queue();
Iterator<Runnable> it = queue.iterator();
while(it.hasNext()){
Runnable h = it.next();
System.out.println("队列中的 "+ h);
}
}
B任务删除成功
A callA start 1601870104613
B callA start 1601870104613
A callA start 1601870105612
删除任务B true
队列中的 urrent.ScheduledThreadPoolExecutor
ScheduledFutureTask@5c647e05队列中的urrent.ScheduledThreadPoolExecutor ScheduledFutureTask@33909752
A callA start 1601870108612
A callA start 1601870109613
C callA start 1601870109613
A callA start 1601870110612
C callA start 1601870110614
tExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value) 停掉任务
甲状腺的症状当ScheduledThreadPoolExecutor执⾏了shutdown()⽅法时,任务是否继续进⾏,默认是true继续执⾏,当传⼊fal时就不会继续执⾏。不能与scheduleWithFixedDelay与scheduleAtFixedRate⽅法进
⾏联⽤(不起作⽤,都会停掉任务)。
public static void main(String[] args){
ScheduledThreadPoolExecutor pool =new ScheduledThreadPoolExecutor(1);
Runnable a =new MyRunnableB("A");
pool.schedule(a,3, TimeUnit.SECONDS);
pool.tExecuteExistingDelayedTasksAfterShutdownPolicy(fal);
pool.shutdown();
System.out.println("任务已经停掉");
}
测试结果
任务已经停掉
去掉pool.tExecuteExistingDelayedTasksAfterShutdownPolicy(fal)后测试结果
任务已经停掉
A callA start 1601870864312
tContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value)⽅法停掉周期性任务
当使⽤scheduleAtFixedRate和scheduleWithFixedDelay⽅法时,如果调⽤ScheduledThreadPoolExecutor执⾏了shutdown()⽅法,任务是否继续进⾏,true继续执⾏,当传⼊fal时就不会继续执⾏。
’
public static void main(String[] args){
ScheduledThreadPoolExecutor pool =new ScheduledThreadPoolExecutor(1);
Runnable a =new MyRunnableB("A");
pool.scheduleAtFixedRate(a,5,5, TimeUnit.SECONDS);
//继续执⾏
// pool.tContinueExistingPeriodicTasksAfterShutdownPolicy(true);
pool.tContinueExistingPeriodicTasksAfterShutdownPolicy(fal);
pool.shutdown();
System.out.println("任务已经停掉");
}
不继续执⾏
任务已经停掉
继续执⾏
民族英雄岳飞
任务已经停掉
A callA start 1601871152082
A callA start 1601871157082
A callA start 1601871162083
cancel(boolean value)与tRemoveOnCancelPolicy(boolean value)
cancel(boolean value) ⽅法是否取消任务,取消任务也不删除队列中的任务(任务不执⾏),正在运作中的任务可以结合
if(Thread.currentThread().isInterrupted()==true)中断任务。
tRemoveOnCancelPolicy(boolean value) ⽅法取消任务后是否将任务从队列中删除。
取消任务,不删除队列中的任务