public class T {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
FutureTask futureTask = new FutureTask(new Callable() {
@Override
public String call() throws Exception {
System.out.println("当前时间:" + sdf.format(new Date())+ " 线程:" + Thread.currentThread().getName() + ": 准备火锅汤底");
Thread.sleep(5000);
return "番茄火锅汤底";
}
});
Thread tr = new Thread(futureTask);
tr.start();
System.out.println("当前时间:" + sdf.format(new Date())+ " 线程:" + Thread.currentThread().getName() + ": 开始准备火锅食材");
Thread.sleep(2000);
System.out.println("当前时间:" + sdf.format(new Date())+ " 线程:" + Thread.currentThread().getName() + ": 火锅食材准备好了");
String tangdi = (String) futureTask.get();
System.out.println("当前时间:" + sdf.format(new Date())+ " 线程:" + Thread.currentThread().getName() + "火锅汤底为:"+ tangdi + " 准备好了,准备开工!");
}
}
控制台输出: