码迷,mamicode.com
首页 > 其他好文 > 详细

future应用相关

时间:2021-06-04 19:55:49      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:pool   adp   execution   result   ade   pre   lex   second   except   

 1 package com.future;
 2 
 3 import java.util.concurrent.*;
 4 
 5 
 6 public class FDemo {
 7     public static final ExecutorService service = Executors.newSingleThreadExecutor();
 8     public static final ExecutorService service1 = Executors.newSingleThreadExecutor();
 9     public static final ExecutorService threadPoolExecutor = new ThreadPoolExecutor(4, 10, 60, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(5));
10 
11     public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
12         Future<Boolean> booleanTask = threadPoolExecutor.submit(() -> {
13             Thread.sleep(3000);
14             return true;
15         });
16         System.out.println("11111111111");
17         // 任务2
18         Future<String> stringTask = threadPoolExecutor.submit(() -> {
19             Thread.sleep(4000);
20             return "Hello World";
21         });
22         //当get的时候开始计算超时,如果不需要考虑超时,可以使用
23         //!booleanTask.isCancelled()&&booleanTask.isDone()来获取值
24         Boolean result1 = booleanTask.get(3, TimeUnit.SECONDS);
25         System.err.println("任务1-10s: " + result1);
26 
27 
28         String result = stringTask.get(3, TimeUnit.SECONDS);
29         System.err.println("任务2-3s: " + result);
30 
31 
32     }
33 
34 
35 }

 

future应用相关

标签:pool   adp   execution   result   ade   pre   lex   second   except   

原文地址:https://www.cnblogs.com/zt1991jskj/p/13745541.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!