Callable
- 优势:有返回值,可以抛出异常
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201205192337678.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ3OTAyMzQ4,size_16,color_FFFFFF,t_70)
- Callable需要经过适配类适配才能丢入new Thread中
public class CallableTest {
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyThread myThread = new MyThread();
FutureTask<Integer> integerFutureTask = new FutureTask<>(myThread);
new Thread(integerFutureTask, "A线程").start();
System.out.println((int)integerFutureTask.get());
}
}
class MyThread implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("call()"