FutureTask配合Thread——FutureTask能够接收Callable类型的参数,用来处理有返回结果的情况

 1 package cn.itcast.test;
 2 
 3 import lombok.extern.slf4j.Slf4j;
 4 
 5 import java.util.concurrent.Callable;
 6 import java.util.concurrent.ExecutionException;
 7 import java.util.concurrent.FutureTask;
 8 
 9 @Slf4j(topic = "c.Test3")
10 public class Test3 {
11     public static void main(String[] args) throws ExecutionException, InterruptedException {
12         FutureTask<Integer> task=new FutureTask<>(new Callable<Integer>() {
13             @Override
14             public Integer call() throws Exception {
15                 log.debug("running~");
16                 Thread.sleep(2000);
17                 return 100;
18             }
19         });
20         Thread t1 = new Thread(task, "t1");
21         t1.start();
22         log.debug("结果是:{}",task.get());
23     }
24 }

 

 

posted @ 2020-05-16 21:52  小草dym  阅读(288)  评论(0)    收藏  举报