package com.yanxizhu;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* @description: FutureTask可用于闭锁
* @date: 2022/3/19 11:48
* @version: 1.0
*/
public class FutureTaskDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
myFutureTask myFutureTask = new myFutureTask();
FutureTask<Integer> futureTask = new FutureTask<>(myFutureTask);
new Thread(futureTask).start();
//必须等线程执行完,才执行下面的代码,因此FutureTask可用于闭锁
Integer integer = futureTask.get();
System.out.println("integer="+integer);
System.out.println("=======================");
}
public static class myFutureTask implements Callable<Integer>{
@Override
public Integer call() throws Exception {
int i=1;
for(;i<1000000;i++){
i++;
}
return i;
}
}
}
评论 (0)