我必须通过Monitor锁来解决这个问题。我做了一些代码,但我的逻辑需要你的建议。在输出中,例如它似乎只检查1人占用浴室的情况和另一个等待它转(我很抱歉我的英语,我试图尽可能地描述)。 这是输出:
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();
}
}
我很感激你的建议 BTW numberOfToilets = 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();
}
}
}
}
答案 0 :(得分:0)
您的代码显示男性和女性不应同时进入浴室。因此,如果不同性别的人想要进入,那么接缝就能正常工作。如果几个性别相同的人想要进入浴室,请尝试它是否正常工作。