Java模拟耗时任务异步执行
2024-08-26 00:44:09
说明:耗时任务开启单独线程处理,任务线程处理完毕通知主线程
1、回调接口定义
public interface ResponseCallBack { public void printMsg(String msg); }
2、模拟耗时任务线程
public class TestMain { public static void main(String[] args){ ExecutorService executorService = Executors.newFixedThreadPool(1); executorService.submit(new TestThread(new ResponseCallBack() { @Override public void printMsg(String msg) { System.out.println("print message: " + msg); } })); executorService.shutdown(); } } class TestThread implements Runnable { private ResponseCallBack responseCallBack; public TestThread(ResponseCallBack responseCallBack) { this.responseCallBack = responseCallBack; } @Override public void run() { try { System.out.println(Thread.currentThread().getName() + " start execute"); Thread.sleep(3000); // 耗时任务 System.out.println(Thread.currentThread().getName() + " end execute"); this.responseCallBack.printMsg("Hello Call Back"); // 耗时任务执行完后,通知主线程 } catch (InterruptedException e) { e.printStackTrace(); } } }
最新文章
- c#文件读入与写入
- openjudge2985(数字组合)
- mybatis 返回null 及 参数说明
- ACM题目————玩转二叉树
- CustomerSOList
- Oracle SGA参数调整
- [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)
- 为网上流行论点“UIAutomator不能通过中文文本查找控件”正名
- 开源纯C#工控网关+组态软件(九)定制Visual Studio
- March 03rd, 2018 Week 9th Saturday
- macOS 10.13 High Sierra odoo11 开发配置--完整版
- PPT文件太大时可以考虑另存为PPTX格式
- poj 1723 SOLDIERS 带权中位数
- 【netcore基础】.Net core自动作业之Hangfire
- java实现PC之间的udp数据单向传输
- 测试oracle数据库连接
- iOS 10 的一个重要更新-线程竞态检测工具 Thread Sanitizer
- eclipse - 新建jsp页面默认模板设置
- dup()&dup2()
- Tcp/ip实验准备:一个简单的定时器——boost实现
热门文章
- Unity3d中如何查找一个脚本被挂在那些预设上面?
- 仙人掌&;圆方树学习笔记
- Python之路(第十七篇)logging模块
- Java基础知识拾遗(一)
- JavaScript IIEF 模仿块级作用域
- SpringBoot系列: SpringBoot Web项目中使用Shiro
- volative 与处理器的嗅探技术
- Linux之搭建远程数据库[Ubuntu:全过程]
- Coursera, Big Data 3, Integration and Processing (week 1/2/3)
- 报文段、协议、MAC地址