标签:public custom str tst 服务 tar cat except col
/** * @Description: 信号量 * @author: fdy * @date: 2020/2/13 16:55 */ public class CustomCheckWIndow { public static void main(String[] args) { // 设置3个信号量,即3个服务窗口 Semaphore semaphore = new Semaphore(3); // 这个队伍排了5个人 for (int i = 1; i <= 5; i++) { new SecurityCheckThread(i,semaphore).start(); } } private static class SecurityCheckThread extends Thread { private int seq; private Semaphore semaphore; public SecurityCheckThread(int seq, Semaphore semaphore) { this.seq = seq; this.semaphore = semaphore; } @Override public void run() { try { semaphore.acquire(); System.out.println("No."+seq+"乘客,正在检验中"); // 假设号码是整除2的人是身份可疑人员,需要花更长时间来安检 if (seq%2 == 0){ Thread.sleep(10000); System.out.println("No."+seq+"乘客,身份可疑,不能出国! "); } } catch (InterruptedException e) { e.printStackTrace(); }finally { semaphore.release(); System.out.println("No."+seq+"乘客已完成服务."); } } } }
只有调用Semaphore对象的acquire()成功后, 才可以往下执行, 完成后执行release()释放持有的信号量.
标签:public custom str tst 服务 tar cat except col
原文地址:https://www.cnblogs.com/fdy-study-consist/p/12304540.html