educative.io

No starvation for each grender

i am trying to come up with a solution in which there’s no possibility of starvation for threads of either gender. any ideas?

package com.linkedin.sample;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import static java.lang.Thread.*;

public class UnisexBathRoom {

final static int MAX_CAPACITY = 2;
private ReentrantLock lock = new ReentrantLock();
private Condition menWaiting = lock.newCondition();
private Condition womenWaiting = lock.newCondition();
private int womenWaitingN = 0;
private int menWaitingN = 0;
private int womenUsingN = 0;
private int menUsingN = 0;
private int free_resources = 0;

public UnisexBathRoom() {

}

public static void main(String[] args) throws InterruptedException {

final UnisexBathRoom unisexBathroom = new UnisexBathRoom();

Thread male1 = new Thread(new Runnable() {
public void run() {
try {
unisexBathroom.acquireMen(“John1”);
} catch (InterruptedException ie) {

}
}
});

Thread male2 = new Thread(new Runnable() {
public void run() {
try {
unisexBathroom.acquireMen(“John2”);
} catch (InterruptedException ie) {

}
}
});

Thread male3 = new Thread(new Runnable() {
public void run() {
try {
unisexBathroom.acquireMen(“John3”);
} catch (InterruptedException ie) {

}
}
});

Thread male4 = new Thread(new Runnable() {
public void run() {
try {
unisexBathroom.acquireMen(“John4”);
} catch (InterruptedException ie) {
}
}
});

Thread male5 = new Thread(new Runnable() {
public void run() {
try {
unisexBathroom.acquireMen(“John5”);
} catch (InterruptedException ie) {
}
}
});

Thread male6 = new Thread(new Runnable() {
public void run() {
try {
unisexBathroom.acquireMen(“John6”);
} catch (InterruptedException ie) {
}
}
});

Thread female1 = new Thread(new Runnable() {
public void run() {
try {
unisexBathroom.acquireWomen(“Lisa1”);
} catch (InterruptedException ie) {
}
}
});

Thread female2 = new Thread(new Runnable() {
public void run() {
try {
unisexBathroom.acquireWomen(“Lisa2”);
} catch (InterruptedException ie) {
}
}
});

Thread female3 = new Thread(new Runnable() {
public void run() {
try {
unisexBathroom.acquireWomen(“Lisa3”);
} catch (InterruptedException ie) {
}
}
});

Thread female4 = new Thread(new Runnable() {
public void run() {
try {
unisexBathroom.acquireWomen(“Lisa4”);
} catch (InterruptedException ie) {
}
}
});

Thread female5 = new Thread(new Runnable() {
public void run() {
try {
unisexBathroom.acquireWomen(“Lisa5”);
} catch (InterruptedException ie) {
}
}
});

Thread female6 = new Thread(new Runnable() {
public void run() {
try {
unisexBathroom.acquireWomen(“Lisa6”);
} catch (InterruptedException ie) {
}
}
});

male1.start();
male2.start();
male3.start();
male4.start();
male5.start();
male6.start();
female1.start();
female2.start();
female3.start();
female4.start();
female5.start();
female6.start();

male1.join();
male2.join();
male3.join();
male4.join();
male5.join();
male6.join();
female1.join();
female2.join();
female3.join();
female4.join();
female5.join();
female6.join();
}

public void acquireMen(String name) throws InterruptedException {

lock.lock();
menWaitingN = menWaitingN + 1;

while (free_resources == MAX_CAPACITY || womenUsingN > 0) {
menWaiting.await();
}

free_resources = free_resources + 1;
menUsingN = menUsingN + 1;
menWaitingN = menWaitingN - 1;
lock.unlock();

System.out.println(name + " is using the bathroom");
useBathRoom();
System.out.println(name + " is done using the bathroom");

lock.lock();
menUsingN = menUsingN - 1;
free_resources = free_resources - 1;
if (womenWaitingN > 0) {
womenWaiting.signal();
} else {
menWaiting.signal();
}

lock.unlock();
}

public void acquireWomen(String name) throws InterruptedException {

lock.lock();

womenWaitingN = womenWaitingN + 1;

while (free_resources == MAX_CAPACITY || menUsingN > 0) {
womenWaiting.await();
}

free_resources = free_resources + 1;
womenUsingN = womenUsingN + 1;
womenWaitingN = womenWaitingN - 1;
lock.unlock();

System.out.println(name + " is using the bathroom");
useBathRoom();
System.out.println(name + " is done using the bathroom");

lock.lock();
womenUsingN = womenUsingN - 1;
free_resources = free_resources - 1;
if (menWaitingN > 0) {
menWaiting.signal();
} else {
womenWaiting.signal();
}

lock.unlock();
}

private void useBathRoom() throws InterruptedException {
sleep(5000);
}
}

Check the hint here:https://stackoverflow.com/questions/11135207/java-unisex-bathroom#:~:text=signal()%3B,opposite%20sex%20(if%20waiting).