博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FutureTask demo springboot
阅读量:5097 次
发布时间:2019-06-13

本文共 5631 字,大约阅读时间需要 18 分钟。

入门:

 public class MutilThreadTest {

/**     * 方式一:callable  有返回值     * @param bb     * @return     * @throws Exception     *///    @Async     public Callable
testCallable1(String bb) throws Exception{ Callable
result= new Callable
() { @Override public String call() throws Exception { System.err.println("线程一启动了"); System.err.println("线程一启动了"); System.err.println("线程一启动了"); Thread.sleep(4000); return bb; }}; return result; } //@Async public Callable
testCallable2(String aa) throws Exception{ Callable
result= new Callable
() { @Override public String call() throws Exception { Thread.sleep(5000); System.err.println("线程2启动了"); System.err.println("线程2启动了"); System.err.println("线程2启动了"); return aa; }}; return result; } @Test//public FutureTask
testFutureTask() throws Exception { public void testFutureTask() throws Exception { long start = System.currentTimeMillis();// Thread.sleep(3000); FutureTask
futureTask1 = new FutureTask
(testCallable1("xain11111"));// 将Callable写的任务封装到一个由执行者调度的FutureTask对象 FutureTask
futureTask2 = new FutureTask
(testCallable2("xain2222")); ExecutorService executor = Executors.newFixedThreadPool(2); // 创建线程池并返回ExecutorService实例 executor.execute(futureTask1); // 执行任务 executor.execute(futureTask2); System.err.println("total cost time is :" +(System.currentTimeMillis()-start));// System.err.println(futureTask1.get());// System.err.println(futureTask2.get());// boolean done = futureTask1.isDone();// boolean done1 = futureTask2.isDone();// System.err.println(done);// System.err.println(done1);// executor.shutdown(); // 关闭线程池和服务 // Thread.currentThread().join();// return; while (true) { //等待所有任务都执行结束 boolean done = futureTask1.isDone(); boolean done1 = futureTask2.isDone(); if(done&&done1){ System.err.println("total cost time is11 :" +(System.currentTimeMillis()-start)); System.err.println(futureTask1.get()); System.err.println(futureTask2.get()); break; } } } }

 

配置线程池:

@Configurationpublic class ThreadPoolConfig {        @Bean    @Qualifier("executorServicePro")    public ExecutorService executorServicePro(){                return Executors.newFixedThreadPool(20);    }}

 

 

 

test: 例子

@Autowired@Qualifier("executorServicePro")private  ExecutorService executorServicePro;public FutureTask
getDataAsync(String word ,long millis,ExecutorService executor){ Callable
callable = new Callable
() {
@Override public String call() throws Exception { long start = System.currentTimeMillis(); logger.info("start query data "); Thread.sleep(millis); logger.info("end query data from db and total cost time is {}",(System.currentTimeMillis()-start)); return word; } }; FutureTask
futureTask = new FutureTask
(callable); executorServicePro.execute(futureTask); //// executor.submit(futureTask); return futureTask;}@Testpublic void testNameqq() throws Exception { ExecutorService executor = Executors.newFixedThreadPool(2); long start = System.currentTimeMillis(); logger.info("zhu xian cheng start "); FutureTask
dataAsync1 = getDataAsync("X1111",2000l,executor); FutureTask
dataAsync2 = getDataAsync("X1111",6000l,executor); FutureTask
dataAsync3 = getDataAsync("X1111",2000l,executor); FutureTask
dataAsync4 = getDataAsync("X1111",1000l,executor);// executor.execute(dataAsync1);// executor.execute(dataAsync2);// executor.execute(dataAsync3);// executor.execute(dataAsync4); //此处一定要全部得到后,然后在get() ,否则线程串行,时间累加 dataAsync1.get(); dataAsync2.get(); dataAsync3.get(); dataAsync4.get(); logger.info("main total cost time is {}",(System.currentTimeMillis()-start)); logger.info("zhu xian cheng end ");}

 

结果:

  

2019-05-17 18:34:38.408 INFO 20768 --- [ main] com.icil.report.MutilThreadTest2 : zhu xian cheng start

2019-05-17 18:34:38.409 INFO 20768 --- [pool-1-thread-1] com.icil.report.MutilThreadTest2 : start query data from db and jdbc is {}
2019-05-17 18:34:38.409 INFO 20768 --- [pool-1-thread-2] com.icil.report.MutilThreadTest2 : start query data from db and jdbc is {}
2019-05-17 18:34:38.410 INFO 20768 --- [pool-1-thread-3] com.icil.report.MutilThreadTest2 : start query data from db and jdbc is {}
2019-05-17 18:34:38.410 INFO 20768 --- [pool-1-thread-4] com.icil.report.MutilThreadTest2 : start query data from db and jdbc is {}
2019-05-17 18:34:39.410 INFO 20768 --- [pool-1-thread-4] com.icil.report.MutilThreadTest2 : end query data from db and total cost time is 1000
2019-05-17 18:34:40.409 INFO 20768 --- [pool-1-thread-1] com.icil.report.MutilThreadTest2 : end query data from db and total cost time is 2000
2019-05-17 18:34:40.410 INFO 20768 --- [pool-1-thread-3] com.icil.report.MutilThreadTest2 : end query data from db and total cost time is 2000
2019-05-17 18:34:44.410 INFO 20768 --- [pool-1-thread-2] com.icil.report.MutilThreadTest2 : end query data from db and total cost time is 6001
2019-05-17 18:34:44.410 INFO 20768 --- [ main] com.icil.report.MutilThreadTest2 : main total cost time is 6002
2019-05-17 18:34:44.410 INFO 20768 --- [ main] com.icil.report.MutilThreadTest2 : zhu xian cheng end

 

转载于:https://www.cnblogs.com/lshan/p/10880862.html

你可能感兴趣的文章
你真的会使用assert吗?
查看>>
Spring配置文件-笔记
查看>>
nginx运行出现 file not found 错误处理原因
查看>>
python语法要素
查看>>
python库编程.os平台.office平台
查看>>
js回调函数(callback)理解
查看>>
.net core学习思路
查看>>
Jade之Template Inheritance
查看>>
Magento2.X 后端开发简要1
查看>>
大话文本检测经典模型:EAST
查看>>
文本主题模型之LDA(一) LDA基础
查看>>
linux基础命令-chgrp/chown/chomd
查看>>
待整理
查看>>
iOS 6
查看>>
Nginx入门篇-基础知识与linux下安装操作
查看>>
一次动态sql查询订单数据的设计
查看>>
C# 类(10) 抽象类.
查看>>
1.linux ping:unknown host www.***.***
查看>>
Swagger自动生成接口文档
查看>>
MyEclipse2016项目内复制一个项目,如何更改项目的访问路径
查看>>