,Java线程Callable_Future_Executors线程池的使用
1.首先创建任务,继承callable接口能返回结果。
package com.currentPro.task; import java.util.concurrent.Callable; public class MuniusTask implements Callable<Integer>{ private Integer number; public MuniusTask (Integer number){ this.number = number; } @Override public Integer call() throws Exception { int result ; result = number - 1; return result; } }
2.创建TaskExecutor,用来创建线程池
public class MuniusTaskExecutor { protected ExecutorService executors; public MuniusTaskExecutor(Integer threadNums){ this.executors = Executors.newFixedThreadPool(threadNums); } }
3.测试
package com.currentPro.task; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; public class TaskTest { public static void main(String[] args) throws InterruptedException, ExecutionException { //创建线程池 MuniusTaskExecutor muniusTaskExecutor = new MuniusTaskExecutor (10); //结果 List<Future<Integer>> resultList = new ArrayList<Future<Integer>> (); Random random = new Random(); for (int i = 0; i < 4; i++) { Integer number = random.nextInt(10); System.out.println("random值:" + number); //创建任务 MuniusTask m1 = new MuniusTask(number); //提交任务 Future<Integer> result = muniusTaskExecutor.executors.submit(m1); resultList.add(result); } for (Future<Integer> future : resultList) { System.out.println("Future result is - " + " - " + future.get() + "; And Task done is " + future.isDone()); } muniusTaskExecutor.executors.shutdown(); } }
4.结果
random值:2 random值:0 random值:2 random值:8 Future result is - - 1; And Task done is true Future result is - - -1; And Task done is true Future result is - - 1; And Task done is true Future result is - - 7; And Task done is true
如果要一次提交多个任务的话,使用completionservice
package com.eminem.test; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class CompletionServiceDemo { public static void main(String[] args) { // 创建线程池 Executor executor = Executors.newFixedThreadPool(10); // 把线程池给予cs,能一次开启多个callable任务 CompletionService cs = new ExecutorCompletionService(executor); for (int i = 1; i <= 10; i++) { final int seq = i; Future future = cs.submit(new Callable<Integer>() { @Override public Integer call() throws Exception { int random = new Random().nextInt(5000); System.out.println("seq" + seq + "休眠了:" + random + "秒"); Thread.sleep(random); return seq; } }); } try { // 依次获取cs的执行任务完后的结果 for (int i = 0; i <= 10; i++) { System.out.println(cs.take().get()); } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }
结果:
seq1休眠了:2002秒
seq3休眠了:603秒
seq5休眠了:1955秒
seq7休眠了:802秒
seq2休眠了:1809秒
seq6休眠了:2247秒
seq4休眠了:2667秒
seq8休眠了:2807秒
seq9休眠了:4604秒
seq10休眠了:503秒
10
3
7
2
5
1
6
4
8
9