多个部门厕所排队问题

场景:公司有若干部门,每个部门一个厕所,每个部门的员工争抢本部门的厕所

 

import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
* @comments 测试main入口
* 场景:公司有若干部门,每个部门一个厕所,每个部门的员工争抢本部门的厕所
* @author Bruce
* @date 2017年11月24日
*/
public class ToiletTester {
	//总人数
	private static final int personNum = 30;
	//所有部门的厕所
	public static Map<Integer, Toilet> DepToilets = new ConcurrentHashMap<>();

	public static void main(String[] args) {
		
		for (int i = 1; i <= personNum; i++) {
			int depId = randomRange(5, 1);//为员工随机分配部门id(从1到5)
			String empName = fixedLengthFill0(3, i) + " 号员工";
			Person person = new Person(depId, empName);
			person.start();
		}

	}

	//------------- 工具方法  ------------------------------
	public static Integer randomRange(int max, int min) {
		return new Random().nextInt(max)%(max-min+1) + min;
	}
	private static String fixedLengthFill0(int length, int seq) {
		StringBuilder strBu = new StringBuilder();
		int fillLen = length - String.valueOf(seq).length();
		for (int i = 0; i < fillLen; i++) {
			strBu.append("0");
		}
		strBu.append(seq);
		return strBu.toString();
	}
}

 

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* @comments 执行的业务方法(被调用的)
* @author Bruce
* @date 2017年11月24日
*/
public class Toilet {
	
	private Integer depId;
	
	private Lock lock = new ReentrantLock(true);
    
	public Toilet(Integer depId) {
		this.depId = depId;
	}
	public void enter() {
		try {
			lock.lock();//拿到锁(锁门)
			System.out.println(Thread.currentThread().getName() + "获得Toilet-DEP"+depId+" 使用权,time:" + System.currentTimeMillis());
			int sec = ToiletTester.randomRange(3, 1);
			Thread.sleep(sec * 1000);//使用1-3秒
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();//释放锁(完事了,开门出去)
		}
		
	}

	//-------------- get & set methods -----------
	public Integer getDepId() {
		return depId;
	}

	public void setDepId(Integer depId) {
		this.depId = depId;
	}
	public Lock getLock() {
		return lock;
	}
	
}

 

/**
* @comments 业务调用者
* @author Bruce
* @date 2017年11月24日
*/
public class Person extends Thread{
	
	private Integer depId;//部门id
	
	private String empName;//员工姓名
	
	public Person(Integer depId, String empName) {
		this.setName("DEP" + depId + "-" + empName);
		this.depId = depId;
		this.empName = empName;
	}

	@Override
	public void run() {
		//如果没有厕所就建立个部门厕所
		ToiletTester.DepToilets.putIfAbsent(depId, new Toilet(depId)); 
		//员工根据所在部门id找到部门的厕所
		Toilet toilet = ToiletTester.DepToilets.get(depId);
		toilet.enter();//进入
		super.run();
	}
	
	//-------------- get & set methods ---------------------------
	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public Integer getDepId() {
		return depId;
	}

	public void setDepId(Integer depId) {
		this.depId = depId;
	}
	
}

 

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值