电话亭问题PV操作实现(Java)

import java.util.Scanner;

//电话亭案例
public class Test {
	public static void main(String[] args) {
		int boy, girl;
		Scanner input = new Scanner(System.in);
		System.out.print("请输入男生数目:");
		boy = input.nextInt();
		System.out.print("请输入女生数目:");
		girl = input.nextInt();
		input.close();
		//实例化男生女生
		for (int i = 0; i < boy; i++) {
			new Thread(new Boy(), "男生" + Integer.toString(i) + "号").start();
		}
		for (int i = 0; i < girl; i++) {
			new Thread(new Girl(), "女生" + Integer.toString(i) + "号").start();
		}
	}
}
//全局对象
class Global {
	public static Semaphore S_mutex = new Semaphore(1);  //互斥信号量
	public static Semaphore S_boys = new Semaphore(0);  //男生等待队列
	public static Semaphore S_girls = new Semaphore(0);  //女生等待队列
	public static int boys_waiting = 0;  //正在等待的男生人数
	public static int girls_waiting = 0;  //正在等待的女生人数
	public static int using = 0;  //当前是否有人在使用电话亭

	//随机等待
	public static void naps() {
		try {
			Thread.sleep((int) (2000 * Math.random()));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
		
}

//男生
class Boy implements Runnable {
	
	public Boy() {
		super();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		Global.naps();
		//男孩想去使用电话
		System.out.println(Thread.currentThread().getName() + "  等待...");
		Global.S_mutex.P();
		if (Global.using == 0 && Global.girls_waiting == 0) {  //没有人在打电话,并且没有女生在等待
			Global.using = 1;
			Global.S_mutex.V();
		} else {
			Global.boys_waiting++;
			Global.S_mutex.V();
			Global.S_boys.P();
		}
		
		//使用电话
		System.out.println(Thread.currentThread().getName() + "--在使用电话..");
		Global.naps();
		
		//男生离开电话亭
		Global.S_mutex.P();
		System.out.println(Thread.currentThread().getName() + "--离开电话亭..");
		if (Global.girls_waiting > 0) {  //优先唤醒女生
			Global.girls_waiting--;
			Global.S_girls.V();
		} else if (Global.boys_waiting > 0) {
			Global.boys_waiting--;
			Global.S_boys.V();
		} else {
			Global.using = 0;  //无人等待
		}
		Global.S_mutex.V();
	}
}

//女生
class Girl implements Runnable {
	
	public Girl() {
		super();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		Global.naps();
		//女孩想去打电话
		System.out.println(Thread.currentThread().getName() + "  等待...");
		Global.S_mutex.P();
		if (Global.using == 0) {  //没有人在打电话
			Global.using = 1;
			Global.S_mutex.V();
		} else {
			Global.girls_waiting++;
			Global.S_mutex.V();
			Global.S_girls.P();
		}
		
		//女生打电话
		System.out.println(Thread.currentThread().getName() + "--在使用电话..");
		Global.naps();
		
		//女生离开电话亭
		Global.S_mutex.V();
		System.out.println(Thread.currentThread().getName() + "--离开电话亭..");
		if (Global.girls_waiting > 0) {  //优先唤醒女生
			Global.girls_waiting--;
			Global.S_girls.V();
		} else if (Global.boys_waiting > 0) {
			Global.boys_waiting--;
			Global.S_boys.V();
		} else {
			Global.using = 0;  //无人等待
		}
		Global.S_mutex.V();
	}
}

//信号量
class Semaphore {
	public int value;

	public Semaphore(int value) {
		super();
		this.value = value;
	}
	//P操作
	public synchronized final void P() {
		// TODO Auto-generated method stub
		value--;
		if(value < 0) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//V操作
	public synchronized final void V() {
		// TODO Auto-generated method stub
		value++;
		if (value <= 0) {
			this.notify();
		}
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值