package service.query;
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();
}
}
線程池創建線程並全部結束後執行主線程
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.