/**
* <html>
* <body>
* <P> Copyright 1994 JsonInternational</p>
* <p> All rights reserved. - https://github.com/Jasonandy/Java-Core-Advanced </p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.core.concurrent; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; /**
* @Package:cn.ucaner.core.concurrent
* @ClassName:CachedThreadPool
* @Description: <p>
* https://blog.csdn.net/agoodcoolman/article/details/44082181
* https://www.zhihu.com/question/23212914
* 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程
*
* 线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程。
* Star : https://www.cnblogs.com/baizhanshi/p/5469948.html</p>
* @Author: - Jason
* @CreatTime:2018年5月16日 下午6:04:59
* @Modify By:
* @ModifyTime: 2018年5月16日
* @Modify marker:
* @version V1.0
*/
public class CachedThreadPool { public static void main(String[] args) { ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
exec.execute(new LiftOff());
}
exec.shutdown();
}
}
/**
* <html>
* <body>
* <P> Copyright 1994 JsonInternational</p>
* <p> All rights reserved. - https://github.com/Jasonandy/Java-Core-Advanced </p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.core.concurrent; import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future; /**
* @Package:cn.ucaner.core.concurrent
* @ClassName:CallableDemo
* @Description: <p> CallableDemo </p>
* @Author: -
* @CreatTime:2018年6月12日 下午3:50:49
* @Modify By:
* @ModifyTime: 2018年6月12日
* @Modify marker:
* @version V1.0
*/
public class CallableDemo { public static void main(String[] args) { ExecutorService exec = Executors.newCachedThreadPool(); ArrayList<Future<String>> results = new ArrayList<>(); for (int i = 0; i < 10; i++) {
results.add(exec.submit(new TaskWithResult(i)));
} for (Future<String> fs : results) {
try {
System.out.println(fs.get());
} catch (InterruptedException e) {
System.out.println(e);
e.printStackTrace();
} catch (ExecutionException e) {
System.out.println(e);
e.printStackTrace();
} finally {
exec.shutdown();
}
}
}
} class TaskWithResult implements Callable<String> {
private int id; public TaskWithResult(int id) {
this.id = id;
} @Override
public String call() throws Exception {
return "result of TaskWithResult " + id;
}
}
//Outputs
//result of TaskWithResult 0
//result of TaskWithResult 1
//result of TaskWithResult 2
//result of TaskWithResult 3
//result of TaskWithResult 4
//result of TaskWithResult 5
//result of TaskWithResult 6
//result of TaskWithResult 7
//result of TaskWithResult 8
//result of TaskWithResult 9
/**
* <html>
* <body>
* <P> Copyright 1994 JsonInternational</p>
* <p> All rights reserved. - https://github.com/Jasonandy/Java-Core-Advanced </p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.core.concurrent; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; /**
* @Package:cn.ucaner.core.concurrent
* @ClassName:CaptureUncaughtException
* @Description: <p> 捕捉异常</p>
* @Author: - Jason
* @CreatTime:2018年5月16日 下午6:10:37
* @Modify By:
* @ModifyTime: 2018年5月16日
* @Modify marker:
* @version V1.0
*/
public class CaptureUncaughtException { public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool(
new HandlerThreadFactory());
exec.execute(new ExceptionThread());
}
} class ExceptionThread implements Runnable {
public void run() {
Thread t = Thread.currentThread();
System.out.println("run() by " + t);
System.out.println(
"eh = " + t.getUncaughtExceptionHandler());
throw new RuntimeException();
}
} class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
public void uncaughtException(Thread t, Throwable e) {
System.out.println("caught " + e + " in " + t);
}
} class HandlerThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) {
System.out.println(this + " creating new Thread");
Thread t = new Thread(r);
System.out.println("created " + t);
t.setUncaughtExceptionHandler(
new MyUncaughtExceptionHandler());
System.out.println(
"eh = " + t.getUncaughtExceptionHandler());
return t;
}
} /* Output: (90% match)
com.brianway.learning.java.concurrent.HandlerThreadFactory@266474c2 creating new Thread
created Thread[Thread-0,5,main]
eh = com.brianway.learning.java.concurrent.MyUncaughtExceptionHandler@6f94fa3e
run() by Thread[Thread-0,5,main]
eh = com.brianway.learning.java.concurrent.MyUncaughtExceptionHandler@6f94fa3e
com.brianway.learning.java.concurrent.HandlerThreadFactory@266474c2 creating new Thread
created Thread[Thread-1,5,main]
eh = com.brianway.learning.java.concurrent.MyUncaughtExceptionHandler@3ff961b5
caught java.lang.RuntimeException in Thread[Thread-0,5,main]
*///:~

最新文章

  1. Android的生命周期学习
  2. vim 创建和管理折叠
  3. HTTP及状态码汇总
  4. int a=5,则 ++(a++)的值是?
  5. php 正则中的&quot;i,m,s,x,e&quot;分别表示什么
  6. [老老实实学WCF] 第二篇 配置WCF
  7. TortoiseSVN显示图标不正常
  8. 从Select语句看Oracle查询原理(了解Oracle的查询机制)
  9. 一步一步重写 CodeIgniter 框架 (7) —— Controller执行时将 Model获得的数据传入View中,实现MVC
  10. JS报表打印分页CSS
  11. 【深度学习与TensorFlow 2.0】卷积神经网络(CNN)
  12. Shell编程(week4_day2)--技术流ken
  13. 使用Windows的mstsc远程桌面连接到Ubuntu图形界面(AWS上安装的Ubuntu系统)
  14. centos7环境开启WIFI热点
  15. Python 全栈开发四 python基础 函数
  16. shell文本处理
  17. 未能加载文件或程序集“XX.XXX.Web”或它的某一个依赖项。试图加载格式不正确的程序
  18. vi文字处理器
  19. gravatar全球通用头像设定
  20. Java导包问题

热门文章

  1. python学习笔记一: 《python3 input()函数》
  2. 设计模式——&lt;面向对象设计原则以及23种设计模式分类&gt;
  3. Hotspot研究-工程结构
  4. 【大数据作业九】安装关系型数据库MySQL 安装大数据处理框架Hadoop
  5. 2D转换模块
  6. xmlns:amq=&quot;http://activemq.apache.org/schema/core&quot;报错
  7. &#39;&gt;/dev/null 2&gt;&amp;1&#39; 是什么意思?
  8. matlab学习笔记10_7数值计算类型和常用计算公式
  9. [LeetCode] 534. Design TinyURL 设计短网址
  10. consul集群搭建以及ACL配置