构造:
public class MyCallable implements Callable<Integer> {
@Override
public Integer call() throws Exception{
return new Random().nextInt();
}
}
测试:
public class TestCallable {
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyCallable myCallable=new MyCallable();
FutureTask<Integer> futureTask=new FutureTask<Integer>(myCallable);
Thread thread = new Thread(futureTask);
thread.start();
Integer integer = futureTask.get();
System.out.println(integer);
}
}
结果: