入门:
public class MutilThreadTest {
/** * 方式一:callable 有返回值 * @param bb * @return * @throws Exception */ // @Async public Callable<String> testCallable1(String bb) throws Exception{ Callable<String> result= new Callable<String>() { @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<String> testCallable2(String aa) throws Exception{ Callable<String> result= new Callable<String>() { @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<String> testFutureTask() throws Exception { public void testFutureTask() throws Exception { long start = System.currentTimeMillis(); // Thread.sleep(3000); FutureTask<String> futureTask1 = new FutureTask<String>(testCallable1("xain11111"));// 将Callable写的任务封装到一个由执行者调度的FutureTask对象 FutureTask<String> futureTask2 = new FutureTask<String>(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; } } }
}
配置线程池:
@Configuration public class ThreadPoolConfig { @Bean @Qualifier("executorServicePro") public ExecutorService executorServicePro(){ return Executors.newFixedThreadPool(20); } }
test: 例子
@Autowired @Qualifier("executorServicePro") private ExecutorService executorServicePro; public FutureTask<String> getDataAsync(String word ,long millis,ExecutorService executor){ Callable<String> callable = new Callable<String>() { @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<String> futureTask = new FutureTask<String>(callable); executorServicePro.execute(futureTask); //// executor.submit(futureTask); return futureTask; } @Test public void testNameqq() throws Exception { ExecutorService executor = Executors.newFixedThreadPool(2); long start = System.currentTimeMillis(); logger.info("zhu xian cheng start "); FutureTask<String> dataAsync1 = getDataAsync("X1111",2000l,executor); FutureTask<String> dataAsync2 = getDataAsync("X1111",6000l,executor); FutureTask<String> dataAsync3 = getDataAsync("X1111",2000l,executor); FutureTask<String> 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