Last active
June 7, 2023 19:01
-
-
Save nil96/e7f964a74c9ce8f3d1f6543e93f58d0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ | |
/*If bathroom is full or occupied by female let person wait*/ | |
while(s.availablePermits()==0 || occupiedBy.equals(FEMALE)){ | |
wait(); | |
} | |
s.acquire(); | |
occupiedBy = MALE; | |
} | |
doToilet(MALE + "_" + name); | |
synchronized (this){ | |
showToilet(); | |
s.release(); | |
/*If bathroom is vacant set bathroom is vacant*/ | |
if(s.availablePermits()==semporeNum){ | |
occupiedBy = NONE; | |
} | |
notifyAll(); | |
} | |
} | |
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(); | |
} | |
s.acquire(); | |
occupiedBy = FEMALE; | |
} | |
doToilet(FEMALE + "_" + name); | |
synchronized (this){ | |
showToilet(); | |
s.release(); | |
/*If bathroom is vacant set bathroom is vacant*/ | |
if(s.availablePermits()==semporeNum){ | |
occupiedBy = NONE; | |
} | |
notifyAll(); | |
} | |
} | |
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++){ | |
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====="); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment