Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java Ahmad and Mohammad are friends and they want to enjoy eating buttered bread

ID: 3818145 • Letter: J

Question

java

Ahmad and Mohammad are friends and they want to enjoy eating buttered bread sandwich prepared by them. They need bread and butter to prepare sandwich. Bread and butter are provided to them by keeping them on a table.

For the aforementioned scenario, develop a program that creates Ahmad and Mohammad as anonymous threads and check if they can prepare sandwich and enjoy eating it for the following conditions.

Ahmad and Mohammad are equally hungry and equally competitive. Both of them have to collect the bread and the butter one at time. Ahmad has in mind to collect (lock) bread first and then collect (lock) butter. Mohammad has in mind to collect (lock) butter first and then collect (lock) bread. Each person needs at least a second to collect the second item after collecting the first item. (Item: bread or butter.). Check if Ahmad and Mohammad are able to prepare sandwich and enjoy it.

Ahmad and Mohammad agree on the condition that they have to always collect bread first and then butter to prepare sandwich.

Explanation / Answer

package com.chegg;

import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;


public class ThreadTest {

   public static void main(String args[]) {
       Vector sharedQueue = new Vector();
       int size = 4;
       Thread prodThread = new Thread(new Mohammad(sharedQueue, size),
               "Mohammad");
       Thread consThread = new Thread(new Ahmad(sharedQueue, size),
               "Ahmad");
       prodThread.start();
       consThread.start();
   }
}

class Mohammad implements Runnable {

   private final Vector sharedQueue;
   private final int SIZE;

   public Mohammad(Vector sharedQueue, int size) {
       this.sharedQueue = sharedQueue;
       this.SIZE = size;
   }

   @Override
   public void run() {
       for (int i = 0; i < 7; i++) {
           System.out.println("Produced: " + i);
           try {
               produce(i);
           } catch (InterruptedException ex) {
               Logger.getLogger(Mohammad.class.getName()).log(Level.SEVERE,
                       null, ex);
           }

       }
   }

   private void produce(int i) throws InterruptedException {

       // wait if queue is full
       while (sharedQueue.size() == SIZE) {
           synchronized (sharedQueue) {
               System.out.println("Queue is full "
                       + Thread.currentThread().getName()
                       + " is waiting , size: " + sharedQueue.size());

               sharedQueue.wait();
           }
       }

       // producing element and notify consumers
       synchronized (sharedQueue) {
           sharedQueue.add(i);
           sharedQueue.notifyAll();
       }
   }
}

class Ahmad implements Runnable {

   private final Vector sharedQueue;
   private final int SIZE;

   public Ahmad (Vector sharedQueue, int size) {
       this.sharedQueue = sharedQueue;
       this.SIZE = size;
   }

   @Override
   public void run() {
       while (true) {
           try {
               System.out.println("Consumed: " + consume());
               Thread.sleep(50);
           } catch (InterruptedException ex) {
               Logger.getLogger(Ahmad .class.getName()).log(Level.SEVERE,
                       null, ex);
           }

       }
   }

   private int consume() throws InterruptedException {
       // wait if queue is empty
       while (sharedQueue.isEmpty()) {
           synchronized (sharedQueue) {
               System.out.println("Queue is empty "
                       + Thread.currentThread().getName()
                       + " is waiting , size: " + sharedQueue.size());

               sharedQueue.wait();
           }
       }

       // Otherwise consume element and notify waiting producer
       synchronized (sharedQueue) {
           sharedQueue.notifyAll();
           return (Integer) sharedQueue.remove(0);
       }
   }
}