import java.util.concurrent.*;
public class BankStatementQuery {
// 指定几个线程
private static final CountDownLatch countDownLatch = new CountDownLatch(3);
public static void main(String[] args) {
BlockingQueue<Runnable> bq = new ArrayBlockingQueue<Runnable>(10);
// 创建线程池
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 5, 50, TimeUnit.MILLISECONDS, bq);
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
try {
// 没有任何问题时,释放线程
countDownLatch.countDown();
System.out.printf("线程一");
} catch (Exception e) {
e.printStackTrace();
}
}
});
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
countDownLatch.countDown();
System.out.printf("线程二");
}
});
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
try {
countDownLatch.countDown();
} catch (Exception e) {
e.printStackTrace();
}
System.out.printf("线程三");
}
});
try {
// 等待所有线程全部释放完成后,再去执行主线程
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.printf("执行完成");
threadPoolExecutor.shutdown();
}
}