1.可以有返回值
2.可以抛出异常
3.方法不同run()/call()
package org.example;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class Cal {
//new Thread(new Runnable() {}).start();
//new Thread(new FutureTask<V>()).start();
//new Thread(new FutureTask<V>(Callable)).start();
public static void main(String[] args) throws ExecutionException, InterruptedException {
//怎么启动callable
MyThread myThread = new MyThread();
FutureTask futureTask = new FutureTask(myThread);//适配类 FutureTask是Runnable的实现类
new Thread(futureTask).start();
Integer o = (Integer) futureTask.get();//获取callable的返回结果
System.out.println(o);
}
}
class MyThread implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("call()");
return 1024;
}
}