public static void main(String[] args) throws Exception{
//主线程
System.out.println("thread start");
//声明semaphore
int count = 10;
Semaphore semaphore = new Semaphore(5);
//创建pool
int corePoolSize = 10;
int maximumPoolSize = 20;
long keepAliveTime = 0;
TimeUnit timeUnit = TimeUnit.SECONDS;
BlockingDeque<Runnable> blockingDeque = new LinkedBlockingDeque<>(10);
ThreadPoolExecutor pool = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,timeUnit,blockingDeque);
//执行
for(int i=0;i<count;i++){
Thread.sleep(1000L);
Future<Long> future = pool.submit(new Callable<Long>() {
@Override
public Long call() throws Exception {
System.out.println(Thread.currentThread().getName() + " begin");
semaphore.acquire(1);
System.out.println(Thread.currentThread().getName() + " action");
Thread.sleep(3000);
semaphore.release();
System.out.println(Thread.currentThread().getName() + " end");
return 1L;
}
});
}
System.out.println("thread end");
}
Semaphore
于 2020-08-21 15:10:57 首次发布