package test;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ChildThreadTest{
public static void main(String[] args){
ExecutorService executorService = Executors.newCachedThreadPool();
Future<Boolean> future = executorService.submit(new Callable<Boolean>() {
// 子线程1
@Override
public Boolean call() throws Exception {
// 子线程1
Future<Boolean> future1 = executorService.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
Thread.sleep(1000);
System.out.println("子线程1执行了...");
return true;
}
});
// 子线程2
Future<Boolean> future2 = executorService.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
System.out.println("子线程2执行了...");
return true;
}
});
// 子线程3
Future<Boolean> future3 = executorService.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
System.out.println("子线程3执行了...");
return true;
}
});
future1.get();
future2.get();
future3.get();
return true;
}
});
try {
// 阻塞
future.get();
System.out.println("外部线程执行了....");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 启动有序关闭,在此过程中执行先前提交的任务,但不接受任何新任务。如果已经关闭,调用不会产生额外的效果。
System.out.println("线程关闭!");
executorService.shutdown();
}
}
}
执行结果: