多线程Java中性浴室监视器
我必须用监视器锁来解决这个问题。我做了一些代码,但我需要你的建议和我的逻辑。例如,在输出中,它似乎只检查一个人占用浴室,另一个人等待轮换的情况(我为我的英语感到抱歉,我正在尽可能地描述)。 以下是输出:
Man 0 enters bathroom
Man 0 in bathroom
Man 0 exits bathroom
Man 0 enters bathroom
Man 0 in bathroom
Man 0 exits bathroom
Woman 1 enters bathroom
Woman 1 in bathroom
Man 2 in waiting------------>>>>
Woman 1 exits bathroom
Man 2 in bathroom
Man 2 exits bathroom
Woman 2 enters bathroom
Woman 2 in bathroom
Woman 2 exits bathroom
下面是4个函数的代码
public void woman_wants_to_enter(int i) throws InterruptedException {
lock.lock();
try {
if (occupiedCount < numberOfToilets) {
if (menUsingN == 0) {
if (womenWaitingN == 0) {
System.out.println("Woman " + i + " enters bathroom ");
womenUsingN++;
occupiedCount++;
} else {
while (womenWaitingN != 0) {
System.out.println("Woman " + i + " in waiting------------>>>>");
womenWaitingN++;
womenWaitingQueue.await();
}
}
} else {
while (menUsingN != 0) {
System.out.println("Woman " + i + " in waiting------------>>>>");
womenWaitingN++;
womenWaitingQueue.await();
}
}
} else {
while (occupiedCount == numberOfToilets) {
System.out.println("Woman " + i + " in waiting------------>>>>");
womenWaitingN++;
womenWaitingQueue.await();
}
}
} finally {
lock.unlock();
}
}
public void woman_leaves(int i) throws InterruptedException {
lock.lock();
try {
womenUsingN--;
occupiedCount--;
System.out.println("Woman " + i + " exits bathroom ");
if (womenWaitingN > 0) {
womenWaitingQueue.signal();
womenUsingN++;
occupiedCount++;
womenWaitingN--;
} else if (menWaitingN > 0 && womenUsingN == 0) {
menWaitingQueue.signal();
menUsingN++;
occupiedCount++;
menWaitingN--;
}
} finally {
lock.unlock();
}
}
public void man_wants_to_enter(int i) throws InterruptedException {
lock.lock();
try {
if (occupiedCount < numberOfToilets) {
if (womenUsingN == 0) {
if (womenWaitingN > 0) {
womenWaitingQueue.signal();
womenUsingN++;
occupiedCount++;
womenWaitingN--;
} else {
menUsingN++;
occupiedCount++;
System.out.println("Man " + i + " enters bathroom ");
menWaitingQueue.signal();
}
} else {
while (womenUsingN != 0) {
System.out.println("Man " + i + " in waiting------------>>>>");
menWaitingN++;
menWaitingQueue.await();
}
}
} else {
while (occupiedCount == numberOfToilets) {
System.out.println("Man " + i + " in waiting------------>>>>");
menWaitingN++;
menWaitingQueue.await();
}
}
} finally {
lock.unlock();
}
}
public void man_leaves(int i) throws InterruptedException {
lock.lock();
try {
menUsingN--;
occupiedCount--;
System.out.println("Man " + i + " exits bathroom ");
if (womenWaitingN > 0 && menUsingN == 0) {
womenWaitingQueue.signal();
womenUsingN++;
occupiedCount++;
womenWaitingN--;
} else if (menWaitingN > 0) {
menWaitingQueue.signal();
menWaitingN--;
menUsingN++;
occupiedCount++;
}
} finally {
lock.unlock();
}
}
谢谢你的建议 顺便说一句,厕所的数量=3
private Lock lock = new ReentrantLock();
private Condition womenWaitingQueue = lock.newCondition();
private Condition menWaitingQueue = lock.newCondition();
private int womenWaitingN = 0;
private int menWaitingN = 0;
private int womenUsingN = 0;
private int menUsingN = 0;
private int numberOfToilets;
private int occupiedCount;
public BathRoom(int numberOfToilets, int occupiedCount) {
this.numberOfToilets = numberOfToilets;
this.occupiedCount = occupiedCount;
}
主要功能
public static void main(String args[]) {
Thread[] women = new Thread[3];
Thread[] men = new Thread[3];
int numberOfToilets = 3;
int occupiedCount = 0;
BathRoom theBathRoom = new BathRoom(numberOfToilets, occupiedCount);
for (int i = 0; i < 3; i++)
women[i] = new Thread(new Woman(i, theBathRoom));
for (int i = 0; i < 3; i++)
men[i] = new Thread(new Man(i, theBathRoom));
for (int i = 0; i < 3; i++)
women[i].start();
for (int i = 0; i < 3; i++)
men[i].start();
}
女性阶级
class Woman implements Runnable {
private int n; /* This identifies the woman. */
private BathRoom theBathRoom;
public Woman(int n, BathRoom b) {
this.n = n;
this.theBathRoom = b;
}
public void run() {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep((long) (500 * Math.random()));
} catch (InterruptedException e) {
}
try {
theBathRoom.woman_wants_to_enter(n);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Woman " + n + " in bathroom ");
try {
Thread.sleep((long) (500 * Math.random()));
} catch (InterruptedException e) {
}
try {
theBathRoom.woman_leaves(n);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} 男子阶级
class Man implements Runnable {
private int n; /* this identifies the man */
private BathRoom theBathRoom;
public Man(int n, BathRoom b) {
this.n = n;
this.theBathRoom = b;
}
public void run() {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep((long) (500 * Math.random()));
} catch (InterruptedException e) {
}
try {
theBathRoom.man_wants_to_enter(n);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Man " + n + " in bathroom ");
try {
Thread.sleep((long) (500 * Math.random()));
} catch (InterruptedException e) {
}
try {
theBathRoom.man_leaves(n);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
# 1 楼答案
你的密码显示男人和女人不应该同时进入浴室。因此,如果不同性别的人想进入,就可以很好地工作。如果有几个同性想要进入浴室,试试看是否有效