java高级---->Thread之ScheduledExecutorService的使用
ScheduledExecutorService的主要作用就是可以将定时任务与线程池功能结合使用。今天我们来学习一下ScheduledExecutorService的用法。我们都太渺小了,那么容易便湮没于各自的殊途。
ScheduledExecutorService的简单使用
一、使用scheduleAtFixedRate()方法实现周期性执行
public class ScheduledExecutorServiceTest {
public static void main(String[] args) {
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("run "+ System.currentTimeMillis());
}
}, 0, 100, TimeUnit.MILLISECONDS);
}
}
运行的结果如下:立刻执行,而且每隔100毫秒执行一次。
run 1501051231331
run 1501051231427
run 1501051231527
run 1501051231628
run 1501051231726
run 1501051231827
run 1501051231926
run 1501051232026
run 1501051232127
.......
二、ScheduledExecutorService使用Callable延迟运行
package com.linux.thread;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class CallableRun {
public static void main(String[] args) {
try {
List<Callable> callableList = new ArrayList<>();
callableList.add(new MyCallableA());
callableList.add(new MyCallableB());
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
ScheduledFuture futureA = executorService.schedule(callableList.get(0), 4L, TimeUnit.SECONDS);
ScheduledFuture futureB = executorService.schedule(callableList.get(1), 4L, TimeUnit.SECONDS);
System.out.println(" X = " + System.currentTimeMillis());
System.out.println("返回值A:" + futureA.get());
System.out.println("返回值B:" + futureB.get());
System.out.println(" Y = " + System.currentTimeMillis());
executorService.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
static class MyCallableA implements Callable<String> {
@Override
public String call() throws Exception{
try {
System.out.println("callA begin " + Thread.currentThread().getName() + ", " + System.currentTimeMillis());
TimeUnit.SECONDS.sleep(3); // 休眠3秒
System.out.println("callA end " + Thread.currentThread().getName() + ", " + System.currentTimeMillis());
} catch (Exception e) {
e.printStackTrace();
}
return "returnA";
}
}
static class MyCallableB implements Callable<String> {
@Override
public String call() throws Exception{
System.out.println("callB begin " + Thread.currentThread().getName() + ", " + System.currentTimeMillis());
System.out.println("callB end " + Thread.currentThread().getName() + ", " + System.currentTimeMillis());
return "returnB";
}
}
}
运行的结果如下:
三、使用scheduleWithFixedDelay()方法实现周期性执行
package com.linux.thread;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class RunMain {
public static void main(String[] args) {
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
System.out.println(" x = " + System.currentTimeMillis());
executorService.scheduleWithFixedDelay(new MyRunable(), 1, 2, TimeUnit.SECONDS);
System.out.println(" y = " + System.currentTimeMillis());
}
static class MyRunable implements Runnable {
@Override
public void run() {
try {
System.out.println(" begin = " + System.currentTimeMillis() + ", name: " + Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(4);
System.out.println(" end = " + System.currentTimeMillis() + ", name: " + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
运行的结果如下:
友情链接
作者:
huhx
出处: www.cnblogs.com/huhx
格言:你尽力了,才有资格说自己的运气不好。
版权:本文版权归作者huhx和博客园共有,欢迎转载。未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
出处: www.cnblogs.com/huhx
格言:你尽力了,才有资格说自己的运气不好。
版权:本文版权归作者huhx和博客园共有,欢迎转载。未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!