모니터 잠금으로이 문제를 해결해야합니다. 나는 약간의 코드를했지만 내 논리로 당신의 조언이 필요합니다. 예를 들어, 출력이 1 사람의 경우에만 화장실을 점령하고 그것을 기다리는 다른 하나 (나는 내 영어에 대한 미안 해요, 내가 할 수있는대로 discrying)를 시도하는 것 같아 보인다. 여기에 출력된다 :Java Unisex 욕실 모니터
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();
}
}
}
}
내가 화장실에 머물러 있도록 1 개 더 Thread.sleep을 추가했으며 다른 사람이 테스트하고 싶고 어쩌면 저에게 고맙게 여길만한 문제에 대한 팁을 줄 수 있다면 code.Anyway에 문제가없는 것 같습니다. 그를. –
이러한 함수를 호출하는 코드를 표시해야 할 수도 있습니다. –
그래서 나머지 코드를 추가했습니다. –