CustomCheckWindow java.util.concurrent.Semaphore

package cn.nyzy;

import java.util.concurrent.Semaphore;



public class CustomCheckWindow {
	public static void main(String[] args) throws InterruptedException {
		//设定3个量,即3个服务窗口
		Semaphore s = new Semaphore(3);
		//这个队伍排了5个人
		for (int i = 1; i <=5; i++) {
			Thread.sleep(100);
			new SecurityCheckThread(i,s).start();
		}

	}

}

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 {
			this.semaphore.acquire();
			System.out.println("第" + this.seq + "乘客,正在接受检查");
			if ((this.seq % 2) == 0) {
				sleep(3000);
				System.out.println("第" + this.seq + "乘客,检查不通过,不能出国...");

			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			System.out.println("第" + this.seq + "乘客服务完毕");
			this.semaphore.release();

		}
	}
}

猜你喜欢

转载自blog.csdn.net/az44yao/article/details/87888897
今日推荐