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();
}
}
}