# Unisex Bathroom problem without semaphore

Could I ask you to evaluate my solution to Unisex Bathroom Problem?

### Problem description

An office has a bathroom that can be used by both men and women, but not both at the same time. If a man is in the bathroom, other men may enter, but any women wishing to use the bathroom should wait for it to be empty. If a woman is in the bathroom, other women may enter, but any men wishing to use the bathroom should wait it to be empty. Each person (man or woman) will spend some time using the bathroom.

Design and implement a concurrent solution to the problem. The program should show when a person (man or woman) enters or exits the bathroom; as well as how many people (men or women) are in the bathroom at the moment. Because it is a space of relatively small size, the bathroom has a limiting capacity of persons C (provided as input via the command line or prefixed as a constant value) that can use it at the same time and the time that each person passes in the bathroom is random and different to each program execution.

### My solution

I tried to also solve a starvation issue and I think I succeeded.

Besides all of that, what is the necessity of semaphores that the problem is related to? Why can't size be simply compared to capacity as I did?

public class Bathroom {

private String inUseBy;

private int size;

private final int capacity;

private final Lock lock = new ReentrantLock();

private final Condition man = lock.newCondition();

private final Condition woman = lock.newCondition();

public Bathroom(final int capacity, final int size) {
this.capacity = capacity;
this.size = size;
inUseBy = "";
}

public void enter(final String sex) throws InterruptedException {
lock.lock();
try {
while (size == capacity || (size > 0 && !sex.equals(inUseBy))) {
if (sex.equals("M")) {
man.await();
} else {
woman.await();
}
}
if (size == 0) {
inUseBy = sex;
}
size++;
} finally {
lock.unlock();
}
System.out.println(sex + " using bathroom. Current employees in bathroom = " + size);
System.out.println(sex + " done using bathroom");
lock.lock();
try {
size--;
if (size == 0) {
if (sex.equals("M")) {
woman.signalAll();
} else {
man.signalAll();
}
} else {
if (sex.equals("M")) {
man.signal();
} else {
woman.signal();
}
}
} finally {
lock.unlock();
}
}

}


System.out.println(sex + " using bathroom. Current employees in bathroom = " + size);


This line is not protected by lock, so it is possible that size can change before the output is generated.

For example, if two 'M' arrive simultaneously, you could get as output:

M using bathroom.  Current employees in bathroom = 2
M using bathroom.  Current employees in bathroom = 2


which omits outputting ... = 1.

inUseBy is left as the last occupant, even when size decreases to 0. In the constructor, it is initialized to "", implying that the empty string might also indicate the bathroom is available to any gender. When you decrease size to zero, you should clear inUseBy.

• Hi, thank you for pointing out about the implementation issues. But they can be fixed fairly easy I think and my idea is correct. My main question is about semaphores. Why is the problem related to semaphores if I did it without semaphore. Jun 27, 2019 at 14:45
• Your link to the problem includes two solutions: one using a lock, the other using semaphores, so I am rather confused why you think it is novel that you can solve the problem without a semaphore. Jun 27, 2019 at 18:55

You have implemented a bathroom that can be occupied by only one type of genital at a time. Therefore it is not a unisex bathroom but a monosex bathroom.

You have labelled sex as "man" and "woman" which are genders. From what I understand the terms you are looking for here are "penis" and "vagina".

The code only identifies entrants as "men" hile all others are gategorized as "women". That is flaw, since the code allows all types of Strings to enter the bathroom. For example "M's that identify as F's". You need to have an unlimited amount of conditions for all self-identified Strings that may want to enter the monosex bathroom or implement some sort of genital inspection and gategorization proxy including an N-to-N mapping of which identities can use the bathroom at the same time.

Please note that StackExchange "does not tolerate any language likely to offend or alienate people based on race, gender, sexual orientation, or religion — and those are just a few examples. When in doubt, just don’t."

If you want an inclusive version of this problem, implement a "watering hole" that can only be entered by animals in the "predator" and "prey" categories at a given time.

• it's kinda disingenuous to imply that bathroom usage for a unisex bathroom should group the people using that bathroom into "predator" and "prey". The whole point of unisex bathrooms is to remove such distinctions by providing a cabin for each facility and possibly a shared area for washing hands. That's what would be "truly inclusive". That being said: I like that you pointed out the issues with the categorization used in the code presented in the question :) Jun 27, 2019 at 11:45
• You have labelled sex as "man" and "woman" which are genders. - not OP, this is how the problem is described and it actually doesn't matter, it's just an example; we should focus on the solution; instead of how to categorize bathroom users correctly (-1). CC: @Vogel612 - it's irrelevant what the so called gender studies think about it; it's not a place to discuss what is normal and what not; Jun 27, 2019 at 11:53
• @Vogel612 My sarcasm dectector is malfunctioning, so I should clarify that the subjects I chose to the last paragraph were completely unrelated to the rest of the comment. I chose them as a naturally occurring and easily understandable resource that is exclusive to user groups. Jun 27, 2019 at 12:27
• Moderator note: This answer has been brought to attention on meta. Jun 28, 2019 at 10:56
• This issue at most warrants a comment about changing social norms potentially invalidating the problem's conception, a comment asking for clarification about what sexes or genders the code should support, or a comment about the potential to offend people holding certain ideologies. There's nothing rude about the problem statement, which isn't even the author's own words. Jul 3, 2019 at 23:11