public class MyTest {
public static void main(String[] args) {
Callable<Integer> callable = () -> {
System.out.println("pre execution ");
Thread.sleep(5000);
int randomNumber = new Random().nextInt(500);
System.out.println("post execution ");
return randomNumber;
};
FutureTask<Integer> futureTask = new FutureTask(callable);
new Thread(futureTask).start();
System.out.println("thread has started");
try {
Thread.sleep(2000);//feature的get方法是异步方法
System.out.println(futureTask.get(1, TimeUnit.MILLISECONDS));
} catch (Exception e) {
e.printStackTrace();
}
}
}
JAVA并发编程学习14(Future模式示例)
最新推荐文章于 2023-04-15 14:39:43 发布