public class ThreeThread implements Callable<Integer> {
public Integer call() throws Exception {
int i = 0;
for (; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
return i;
}
public static void main(String[] args) {
ThreeThread threeThread = new ThreeThread();
FutureTask<Integer> futureTask = new FutureTask<Integer>(threeThread);
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
new Thread(futureTask, "返回值的线程").start();
try {
System.out.println("线程返回值: " + futureTask.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
近期评论