一:java线程池并行
import org.junit.Test; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class ThreadTesst01 { @Test public void test01() throws InterruptedException { // 创建线程池 ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(8, 16, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1024)); int taskCount = 100; CountDownLatch countDownLatch = new CountDownLatch(taskCount); for (int i = 0; i < taskCount; i++) { threadPoolExecutor.submit(() -> { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ">>执行结束!"); countDownLatch.countDown(); }); } // 判断是不是所有任务执行结束 countDownLatch.await(); System.out.println("执行完成"); } }
标签:java,并行,util,concurrent,线程,CountDownLatch,import From: https://www.cnblogs.com/hu0529/p/17196282.html