FutureTask demo springboot

入门:

 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

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值