package com.leasehouse;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class TestSemaphore {
static Semaphore semaphore = new Semaphore(10); //这里设置只有10个线程可以并行执行
static ExecutorService executorPool = Executors.newFixedThreadPool(30); //模拟30个线程
public static void main(String[] args) {
for(int i = 0;i< 30;i++) {
executorPool.execute(new Runnable() {
public void run() {
try {
semaphore.acquire();
System.out.println("save data...");
semaphore.release();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
executorPool.shutdown();
}
}