Skip to content

Instantly share code, notes, and snippets.

@nil96
Last active June 7, 2023 19:01

Revisions

  1. nil96 revised this gist Jun 7, 2023. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions Unisex Bathroom Problem
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,7 @@ class Toilet{
    ArrayList<String> arrString = new ArrayList<>();
    public void enterMale(String name) throws InterruptedException {
    synchronized (this){
    /*If bathroom is full or occupied by female let person wait*/
    while(s.availablePermits()==0 || occupiedBy.equals(FEMALE)){
    wait();
    }
    @@ -25,6 +26,7 @@ class Toilet{
    synchronized (this){
    showToilet();
    s.release();
    /*If bathroom is vacant set bathroom is vacant*/
    if(s.availablePermits()==semporeNum){
    occupiedBy = NONE;
    }
    @@ -34,6 +36,7 @@ class Toilet{

    public void enterFemale(String name) throws InterruptedException {
    synchronized (this){
    /*If bathroom is full or occupied by male let person wait*/
    while(s.availablePermits()==0 || occupiedBy.equals(MALE)){
    wait();
    }
    @@ -46,6 +49,7 @@ class Toilet{
    synchronized (this){
    showToilet();
    s.release();
    /*If bathroom is vacant set bathroom is vacant*/
    if(s.availablePermits()==semporeNum){
    occupiedBy = NONE;
    }
    @@ -54,20 +58,23 @@ class Toilet{
    }

    public void doToilet(String candidate) throws InterruptedException {
    /*taking lock on arrString because we are doing addition and substraction here*/
    synchronized (arrString) {
    System.out.println("Candidate " + candidate + " Entered to do toilet ");
    arrString.add(candidate);
    }

    Thread.sleep(5);

    /*taking lock on arrString because we are doing addition and substraction here*/
    synchronized (arrString){
    arrString.removeIf(element -> element.equals(candidate));
    System.out.println("Candidate " + candidate + " left from toilet ");
    }
    }

    public void showToilet(){
    /*taking lock on arrString because we are doing addition and substraction here*/
    synchronized (arrString) {
    System.out.println("\n-------------------------------------------------------\n");
    for(int i=0;i<arrString.size();i++){
  2. nil96 created this gist Jun 7, 2023.
    204 changes: 204 additions & 0 deletions Unisex Bathroom Problem
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,204 @@
    import java.util.ArrayList;
    import java.util.concurrent.Semaphore;

    class Toilet{
    int semporeNum = 40;
    Semaphore s = new Semaphore(semporeNum);
    String MALE = "MALE";
    String FEMALE = "FEMALE";
    String NONE = "NONE";
    String occupiedBy = NONE;
    ArrayList<String> arrString = new ArrayList<>();
    public void enterMale(String name) throws InterruptedException {
    synchronized (this){
    while(s.availablePermits()==0 || occupiedBy.equals(FEMALE)){
    wait();
    }
    s.acquire();
    occupiedBy = MALE;
    }

    doToilet(MALE + "_" + name);



    synchronized (this){
    showToilet();
    s.release();
    if(s.availablePermits()==semporeNum){
    occupiedBy = NONE;
    }
    notifyAll();
    }
    }

    public void enterFemale(String name) throws InterruptedException {
    synchronized (this){
    while(s.availablePermits()==0 || occupiedBy.equals(MALE)){
    wait();
    }
    s.acquire();
    occupiedBy = FEMALE;
    }


    doToilet(FEMALE + "_" + name);
    synchronized (this){
    showToilet();
    s.release();
    if(s.availablePermits()==semporeNum){
    occupiedBy = NONE;
    }
    notifyAll();
    }
    }

    public void doToilet(String candidate) throws InterruptedException {
    synchronized (arrString) {
    System.out.println("Candidate " + candidate + " Entered to do toilet ");
    arrString.add(candidate);
    }

    Thread.sleep(5);

    synchronized (arrString){
    arrString.removeIf(element -> element.equals(candidate));
    System.out.println("Candidate " + candidate + " left from toilet ");
    }
    }

    public void showToilet(){
    synchronized (arrString) {
    System.out.println("\n-------------------------------------------------------\n");
    for(int i=0;i<arrString.size();i++){
    System.out.print( " " + arrString.get(i) + " ");
    }
    System.out.println("\n-------------------------------------------------------\n");
    }
    }
    }


    public class ToiletProblem {

    public static void main(String []args) throws Exception
    {
    String MALE = "MALE";
    String FEMALE = "FEMALE";
    String NONE = "NONE";
    String occupiedBy = NONE;

    Toilet toilet = new Toilet();

    Thread t1 = new Thread(new Runnable() {
    @Override
    public void run() {
    try{
    for(int i=0;i<5;i++){
    System.out.println("Trying to enter toilet " + "MALE" + i);
    toilet.enterMale("MALE" + i);
    // toilet.showToilet();
    }
    }catch (InterruptedException ex){

    }
    }
    });

    Thread t2 = new Thread(new Runnable() {
    @Override
    public void run() {
    try{
    for(int i=0;i<5;i++){
    System.out.println("Trying to enter toilet " + " FEMALE " + i);
    toilet.enterFemale("FEMALE" + i);
    // toilet.showToilet();
    }
    }catch (InterruptedException ex){

    }
    }
    });


    Thread t3 = new Thread(new Runnable() {
    @Override
    public void run() {
    try{
    for(int i=51;i<56;i++){
    System.out.println("Trying to enter toilet " + "MALE" + i);
    toilet.enterMale("MALE" + i);
    // toilet.showToilet();
    }
    }catch (InterruptedException ex){

    }
    }
    });


    Thread t4 = new Thread(new Runnable() {
    @Override
    public void run() {
    try{
    for(int i=51;i<56;i++){
    System.out.println("Trying to enter toilet " + "FEMALE" + i);
    toilet.enterFemale("FEMALE" + i);
    // toilet.showToilet();
    }
    }catch (InterruptedException ex){

    }
    }
    });


    Thread t5 = new Thread(new Runnable() {
    @Override
    public void run() {
    try{
    for(int i=1000;i<1050;i++){
    System.out.println("Trying to enter toilet " + "MALE" + i);
    toilet.enterMale("MALE" + i);
    // toilet.showToilet();
    }
    }catch (InterruptedException ex){

    }
    }
    });


    Thread t6 = new Thread(new Runnable() {
    @Override
    public void run() {
    try{
    for(int i=1000;i<1050;i++){
    System.out.println("Trying to enter toilet " + "FEMALE" + i);
    toilet.enterFemale("FEMALE" + i);
    // toilet.showToilet();
    }
    }catch (InterruptedException ex){

    }
    }
    });

    t1.start();
    t2.start();
    t3.start();
    t4.start();
    t5.start();
    t6.start();


    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();
    t6.join();

    System.out.print("Sunil Singh=====");
    }
    }