package com.atguigu.javademo.juc;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
public class JUCDEmo3 {
public static void main(String[] args) throws Exception{
Semaphore semaphore = new Semaphore(3) ;
for(int x = 0 ;x<20 ;x++){
new Thread(()->{
try{
semaphore.acquire();//尝试进行资源的获取,如果没有获取到则阻塞
if(semaphore.availablePermits() >0){
System.out.println(“资源抢占成功,当前的人员姓名为:”+Thread.currentThread().getName());
}else{
System.err.println(“资源抢占失败,当前的人员姓名为:”+Thread.currentThread().getName());
}
System.out.println("=业务办理开始,当前的人员姓名为:"+Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(2);
System.out.println("业务办理结束=,当前的人员姓名为:"+Thread.currentThread().getName());
semaphore.release();
}catch (Exception e){}
},“业务办理人员-”+x).start();
}
}
}



