入门:
public class MutilThreadTest {
/** * 方式一:callable 有返回值 * @param bb * @return * @throws Exception */// @Async public CallabletestCallable1(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 FutureTaskgetDataAsync(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 10002019-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 20002019-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 20002019-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 60012019-05-17 18:34:44.410 INFO 20768 --- [ main] com.icil.report.MutilThreadTest2 : main total cost time is 60022019-05-17 18:34:44.410 INFO 20768 --- [ main] com.icil.report.MutilThreadTest2 : zhu xian cheng end