说明:耗时任务开启单独线程处理,任务线程处理完毕通知主线程

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();

        }

    }

}

最新文章

  1. c#文件读入与写入
  2. openjudge2985(数字组合)
  3. mybatis 返回null 及 参数说明
  4. ACM题目————玩转二叉树
  5. CustomerSOList
  6. Oracle SGA参数调整
  7. [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)
  8. 为网上流行论点“UIAutomator不能通过中文文本查找控件”正名
  9. 开源纯C#工控网关+组态软件(九)定制Visual Studio
  10. March 03rd, 2018 Week 9th Saturday
  11. macOS 10.13 High Sierra odoo11 开发配置--完整版
  12. PPT文件太大时可以考虑另存为PPTX格式
  13. poj 1723 SOLDIERS 带权中位数
  14. 【netcore基础】.Net core自动作业之Hangfire
  15. java实现PC之间的udp数据单向传输
  16. 测试oracle数据库连接
  17. iOS 10 的一个重要更新-线程竞态检测工具 Thread Sanitizer
  18. eclipse - 新建jsp页面默认模板设置
  19. dup()&dup2()
  20. Tcp/ip实验准备:一个简单的定时器——boost实现

热门文章

  1. Unity3d中如何查找一个脚本被挂在那些预设上面?
  2. 仙人掌&圆方树学习笔记
  3. Python之路(第十七篇)logging模块
  4. Java基础知识拾遗(一)
  5. JavaScript IIEF 模仿块级作用域
  6. SpringBoot系列: SpringBoot Web项目中使用Shiro
  7. volative 与处理器的嗅探技术
  8. Linux之搭建远程数据库[Ubuntu:全过程]
  9. Coursera, Big Data 3, Integration and Processing (week 1/2/3)
  10. 报文段、协议、MAC地址