使用示例
class MyThread implements Callable<String> {
@Override
public String call() throws Exception {
for ( int x = 0 ; x < 10 ; x ++ ) {
System.out.println("******线程执行,x = " + x);
}
return "线程执行完毕!";
}
}
public class Test {
public static void main(String[] args) throws Exception{
FutureTask futureTask = new FutureTask(new MyThread());
new Thread(futureTask).start();
System.out.println("线程返回值:" + futureTask.get());
}
}
Callable 源码
package java.util.concurrent;
@FunctionalInterface
public interface Callable<V> {
V call() throws Exception;
}