import java.util.concurrent.Semaphore; public class UnisexBathroom1 { public sta
ID: 3677108 • Letter: I
Question
import java.util.concurrent.Semaphore;
public class UnisexBathroom1 {
public static void main(String[] args) {
if (args.length != 2) {
printUsage();
}
int totMen = 0;
int totWomen = 0;
try {
totMen = Integer.parseInt(args[0]);
totWomen = Integer.parseInt(args[1]);
}
catch (NumberFormatException e) {
printUsage();
}
System.out.println("Working Entering In Bathroom Leaving");
System.out.println("----------------------------------------------------------");
Thread[] men = new Thread[totMen];
for (int i = 0; i < totMen; i++) {
men[i] = new ManThread(i);
men[i].start();
}
Thread[] women = new Thread[totWomen];
for (int i = 0; i < totWomen; i++) {
women[i] = new WomanThread(i);
women[i].start();
}
for (int i = 0; i < totMen; i++) {
try {
men[i].join();
}
catch (InterruptedException e) {
}
}
for (int i = 0; i < totWomen; i++) {
try {
women[i].join();
}
catch (InterruptedException e) {
}
}
System.exit(0);
}
private static void printUsage() {
System.out.println("Usage: java UnisexBathroom <totMen> <totWomen>");
System.out.println(" <totMen>: Total number of men.");
System.out.println(" <totWomen>: Total number of women.");
System.exit(-1);
}
public static void randomSleep(int max) {
try {
Thread.sleep((int) (Math.random() * max));
}
catch (InterruptedException e) {
}
}
private static class ManThread extends Thread {
private int id;
public ManThread(int id) {
this.id = id;
}
public void run() {
doWork();
useBathroom();
doWork();
}
private void doWork() {
System.out.println("Man " + id);
UnisexBathroom1.randomSleep(10000);
}
private void useBathroom() {
System.out.println(" Man " + id);
UnisexBathroom1.randomSleep(100);
System.out.println(" Man " + id);
UnisexBathroom1.randomSleep(500);
System.out.println(" Man " + id);
UnisexBathroom1.randomSleep(100);
}
}
private static class WomanThread extends Thread {
private int id;
public WomanThread(int id) {
this.id = id;
}
public void run() {
doWork();
useBathroom();
doWork();
}
private void doWork() {
System.out.println("Woman " + id);
UnisexBathroom1.randomSleep(10000);
}
private void useBathroom() {
System.out.println(" Woman " + id);
UnisexBathroom1.randomSleep(100);
System.out.println(" Woman " + id);
UnisexBathroom1.randomSleep(500);
System.out.println(" Woman " + id);
UnisexBathroom1.randomSleep(100);
}
}
}
Please I wanna a correct answer with output , if you can not let other expert do it..please correct answer..
Explanation / Answer
Main.java:80: error: modifier static not allowed here private static class ManThread extends Thread { ^ Main.java:109: error: modifier static not allowed here private static class WomanThread extends Thread { ^ Main.java:36: error: non-static variable this cannot be referenced from a static context men[i] = new ManThread(i); ^ Main.java:42: error: non-static variable this cannot be referenced from a static context
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.