Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Unisex bathroom using semaphores & monitors

I've been set an assignment for concurrent programming, to code a Unisex Toilet. It seems to be a common enough assignment for this subject. For those unfamiliar, the rule are set (in this case at least)

  • The bathroom can be used by both men and women but not both at the same time.
  • The most people you can have in the toilet at once are 5.
  • If you use a semaphore, you must implement it yourself.

I haven’t coded in a long time before this subject, and my knowledge is rusty. I initially coded this up with just counters, and had problems understanding the flow of information though the programme and that version got nowhere!

So I’m starting again, and am looking to know what general way I should go about this. My initial idea is to have 1 toilet, implemented with a binary semaphore, with monitor for the bathroom, limited to 5.

I've also read that the idea behind the problem lends its self best to an implmentation where each person is a thread. If this is the case, it could be messy, as I've tried thread pooling once (few week back) and it never ran for me. :-/

For the moment I’ve no code to show, so an outline as to how things are set up is my biggest concern.

like image 923
Random Coder Avatar asked Mar 27 '26 02:03

Random Coder


1 Answers

Looks like this problem is already been solved in java by Cormac Redmond

like image 131
Amit Deshpande Avatar answered Mar 29 '26 14:03

Amit Deshpande