public class TestCallable {
public static void main(String[] args) throws Exception, ExecutionException {
// TODO Auto-generated method stub
ExecutorService executorService = Executors.newSingleThreadExecutor();
Task task= new Task();
//提交任务并获取执行结果
Future<String> result = executorService.submit(task);
if(result.isDone()) {
System.out.println("===========");
System.out.println(result.get());
}
executorService.shutdown();
System.out.println(Thread.currentThread().getName() + "线程结束");
}
}
import java.util.concurrent.Callable;
public class Task implements Callable<String> {
@Override
public String call() throws Exception {
System.out.println(Thread.currentThread().getName() + "线程開始");
Thread.currentThread().sleep(3000);
System.out.println(Thread.currentThread().getName() + "线程结束");
return "1111";
}
}