信号量
public static void main(String[] args) {// 信号量Semaphore semaphore = new Semaphore(5);//定义只能有五个线程进行操作 如果多的只能等待里面有线程执行完毕后 才能进去执行for (int i = 0; i < 10; i++) {new Pay(semaphore).start();}}static class Pay extends Thread {Semaphore semaphore;public Pay(Semaphore semaphore) {this.semaphore = semaphore;}@Overridepublic void run() {try {// 申请资源semaphore.acquire();// =========================System.out.println("[" + Thread.currentThread().getName() + " ] 正在进行支付");Thread.sleep(3000);// =========================//释放资源semaphore.release();System.out.println("[" + Thread.currentThread().getName() + " ] 支付完成");} catch (Exception e) {e.printStackTrace();}}}
