Java线程池示例:缓存线程池

矫情吗;* 2023-10-08 09:27 222阅读 0赞
  1. private static void testCacheThreadPool() throws InterruptedException {
  2. ExecutorService service = Executors.newCachedThreadPool();
  3. System.out.println(service);
  4. for (int i = 0; i < 2; i++) {
  5. service.execute(()->{
  6. try {
  7. TimeUnit.MILLISECONDS.sleep(500);
  8. } catch (InterruptedException e) {
  9. e.printStackTrace();
  10. }
  11. System.out.println(Thread.currentThread().getName());
  12. });
  13. }
  14. System.out.println(service);
  15. TimeUnit.SECONDS.sleep(80);//每个线程的存货时间为60秒
  16. System.out.println(service);
  17. }

2019110912380578.png

发表评论

表情:
评论列表 (有 0 条评论,222人围观)

还没有评论,来说两句吧...

相关阅读