有返回值的线程

  • 实现CallAble接口
  • 实例:
@SuppressWarnings("unchecked")
public class CallAbleTest {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        CTest c1 = new CTest("a");
        CTest c2 = new CTest("b");
        ExecutorService e = Executors.newFixedThreadPool(2);
        Future<String> f1 = e.submit(c1);
        Future<String> f2 = e.submit(c2);
        System.out.println(f1.get());
        System.out.println(f2.get());
        e.shutdown();
    }
    
    public static class CTest implements Callable{
        
        private String name;
        
        public CTest(String name) {
            this.name = name;
        }
        
        public Object call() throws Exception {
            return name+"返回值";
        }
        
    }
}
上一篇:BERT生成能力改进:分离对话生成和对话理解


下一篇:006 this指针原理