1.10个线程并发执行,最大并发是5
package com.example.demo;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class CountDownTest {
public static int count = 0;
public static void main(String[] args) throws InterruptedException{
ExecutorService executorService = Executors.newFixedThreadPool(100);
final Semaphore semaphore = new Semaphore(5);
final CountDownLatch countDownLatch = new CountDownLatch(10);
for(int i =0;i<10;i++){
executorService.execute(new Runnable() {
@Override
public void run() {
try {
semaphore.acquire();
System.out.println("hello");
Thread.sleep(5000);
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
}
});
}
countDownLatch.await();
executorService.shutdown();
System.out.println("执行完成");
}
}