I have this code below and I only have to correctly display the available burgers and customer waiting. In what way should be edited so that it will have the same output as the example? class Producer implements Runnable { private final int burgers; public Producer(int burgers) { this.burgers = burgers; } @Override public void run() { for (int i = 1; i <= burgers; i++) { // First increment avail burgers MidExam.addAvailBurgers(true); // If there are avail burgers and there are waiting, decrement waiting and decrement avail burgers if (MidExam.getAvailBurgers() > 0 && MidExam.getWaiting() > 0) { MidExam.addWaiting(false); MidExam.addAvailBurgers(false); } System.out.printf("Producer is cooking burger #%d\t\t\t# of Available burgers [%d] Customer waiting: [%d]%n", i, MidExam.getAvailBurgers(), MidExam.getWaiting()); try { Thread.sleep(500); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } } } class Consumer implements Runnable { private final String name; private final int burgers; public Consumer(int b, String n) { burgers = b; name = n; } @Override public synchronized void run() { for (int i = 1; i <= burgers; i++) { try { // If there are avail burgers, decrement waiting and avail burgers if (MidExam.getAvailBurgers() > 0) { MidExam.addWaiting(false); MidExam.addAvailBurgers(false); } // else increment waiting else { MidExam.addWaiting(true); } System.out.printf("Consumer #%d of %s buys 1 burger\t# of Available burgers [%d] Customer waiting: [%d]%n", i, name, MidExam.getAvailBurgers(), MidExam .getWaiting()); Thread.sleep(500); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } } } public class MidExam { //private static final AtomicInteger tempAvail = new AtomicInteger(0); //private static final AtomicInteger tempWait = new AtomicInteger(0); private static int avail = 0; private static int wait = 0; public static int getAvailBurgers() { return avail; } public static int getWaiting() { return wait; } public static synchronized void addAvailBurgers(boolean isAdd) { if (isAdd) { avail++; } else { avail = (avail > 0) ? avail - 1 : 0; } } public static synchronized void addWaiting(boolean isAdd) { if (isAdd) { wait++; } else { wait = (wait > 0) ? wait - 1 : 0; } } public static void main(String[] args) { int burgers = (int) Math.floor(Math.random() * (6 - 1 + 1) + 1); System.out.printf("# of burgers to cook per Customer = %d %n%n", burgers); Thread producer = new Thread(new Producer(burgers * 2)); producer.start(); Thread c1 = new Thread(new Consumer(burgers, "Thread #1")); c1.start(); Thread c2 = new Thread(new Consumer(burgers, "Thread #2")); c2.start(); } }
I have this code below and I only have to correctly display the available burgers and customer waiting. In what way should be edited so that it will have the same output as the example?
class Producer implements Runnable {
private final int burgers;
public Producer(int burgers) {
this.burgers = burgers;
}
@Override
public void run() {
for (int i = 1; i <= burgers; i++) {
// First increment avail burgers
MidExam.addAvailBurgers(true);
// If there are avail burgers and there are waiting, decrement waiting and decrement avail burgers
if (MidExam.getAvailBurgers() > 0 && MidExam.getWaiting() > 0) {
MidExam.addWaiting(false);
MidExam.addAvailBurgers(false);
}
System.out.printf("Producer is cooking burger #%d\t\t\t# of Available burgers [%d] Customer waiting: [%d]%n",
i, MidExam.getAvailBurgers(), MidExam.getWaiting());
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
class Consumer implements Runnable {
private final String name;
private final int burgers;
public Consumer(int b, String n) {
burgers = b;
name = n;
}
@Override
public synchronized void run() {
for (int i = 1; i <= burgers; i++) {
try {
// If there are avail burgers, decrement waiting and avail burgers
if (MidExam.getAvailBurgers() > 0) {
MidExam.addWaiting(false);
MidExam.addAvailBurgers(false);
}
// else increment waiting
else {
MidExam.addWaiting(true);
}
System.out.printf("Consumer #%d of %s buys 1 burger\t# of Available burgers [%d] Customer waiting: [%d]%n",
i, name, MidExam.getAvailBurgers(), MidExam .getWaiting());
Thread.sleep(500);
} catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
}
}
}
public class MidExam {
//private static final AtomicInteger tempAvail = new AtomicInteger(0);
//private static final AtomicInteger tempWait = new AtomicInteger(0);
private static int avail = 0;
private static int wait = 0;
public static int getAvailBurgers() {
return avail;
}
public static int getWaiting() {
return wait;
}
public static synchronized void addAvailBurgers(boolean isAdd) {
if (isAdd) {
avail++;
}
else {
avail = (avail > 0) ? avail - 1 : 0;
}
}
public static synchronized void addWaiting(boolean isAdd) {
if (isAdd) {
wait++;
}
else {
wait = (wait > 0) ? wait - 1 : 0;
}
}
public static void main(String[] args) {
int burgers = (int) Math.floor(Math.random() * (6 - 1 + 1) + 1);
System.out.printf("# of burgers to cook per Customer = %d %n%n", burgers);
Thread producer = new Thread(new Producer(burgers * 2));
producer.start();
Thread c1 = new Thread(new Consumer(burgers, "Thread #1"));
c1.start();
Thread c2 = new Thread(new Consumer(burgers, "Thread #2"));
c2.start();
}
}
Step by step
Solved in 6 steps with 6 images