Callable 有返回值
Runnable 没有返回值
Callable使用
public class C {
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyCallable myCallable = new MyCallable();
// 示例1
FutureTask future1 = new FutureTask<Integer>(myCallable);
new Thread(future1,"A").start();
Integer integer = (Integer) future1.get();
System.out.println(integer);//1020
// 示例2 Lambda写法
FutureTask future2 = new FutureTask<String>(()->{
return "测试";
});
new Thread(future2,"B").start();
String str = (String) future2.get();
System.out.println(str);//测试
}
}
class MyCallable implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println("call()");
return 1020;
}
}