• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronous thread

 
kelvin kimutai
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
want to implement if,or do loop statements in my codes so that the two keeps waiting for each other i.e the cook produces four orders and place them in the shelf. Then waits for the waitress to remove them. On the other side the waitress waits if there is no order on the shelf.

The shelf has four slots serviced by two cooks and four waitress.

in the below scenario i have implemented the place and take as when the cook places the order itis immediately taken by the waitress. This should not be so since there are two cooks and four waitress. there should be instances where one task sleeps or waits for the other.

import java.util.*;
import java.io.*;

// We would like to synchronize cook and waitress so that
// cook puts a order in the shelf, then the waitress takes it
// out, then the cook puts another order, and so on.

// This solution provides the right behaviour
// We have changed the class Shelf to include wait() and notify()
// We also have changed the cook and waitres classes
// slightly to handle a new exception

public class WaitressCookShelf {

public static void main (String [] args) {
Shelf shelf = new Shelf();

// create new threads
Thread cook = new Cook(2, shelf);
Thread waitress = new Waitress(4, shelf);

// starting threads
cook.start();
waitress.start();

// Wait for the threads to finish
try {
cook.join();
waitress.join();
} catch (InterruptedException e) {return;}
}

}

class Shelf {
private int contents;
private boolean empty = true;

public synchronized void put (int i) throws InterruptedException {
while (empty == false) { //wait till the shelf becomes empty
try { wait(); }
catch (InterruptedException e) {throw e;}
}
contents = i;
empty = false;
System.out.println("Cook: put..." + i);
notify();
}

public synchronized int get () throws InterruptedException {
while (empty == true) {//wait till something appears in the shelf
try { wait(); }
catch (InterruptedException e) {throw e;}
}
empty = true;
notify();
int val = contents;
System.out.println("waitress: got..." + val);
return val;
}
}


class Cook extends Thread {
private int n;
private Shelf cookShelf;

public Cook (int m, Shelf shelf) {
n = m;
cookShelf = shelf;
}

public void run() {
for (int i = 0; i < n; i++) {
try {
Thread.sleep( (int) Math.random() * 100); // sleep for a randomly chosen time
} catch (InterruptedException e) {return;}

try {
cookShelf.put(i + 1); //starting from 1, not 0
} catch (InterruptedException e) {return;}

}
}
}

class Waitress extends Thread {
private int n;
private Shelf waitressShelf;

public Waitress (int m, Shelf shelf) {
n = m;
waitressShelf = shelf;
}

public void run() {
int value;
for (int i = 0; i < n; i++) {
try {
value = waitressShelf.get();
} catch (InterruptedException e) {return;}
try {
Thread.sleep( (int) Math.random() * 100); // sleep for a randomly chosen time
} catch (InterruptedException e) {return;}

}
}
}
________________________________________
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to simulate an actual restaurent scenario, you need some more entities in here, eg: client, table queue, receptionist, etc. Anyways, I leave that to you to think and implement it.
One glaring problem is that in your implementation every cook and waitress are not different threads i.e. you have a single thread for both cooks and other for 4 waitresses.
Answering your question:
This should not be so since there are two cooks and four waitress. there should be instances where one task sleeps or waits for the other.

You have made the waitress/cook to sleep for sometime after taking/putting the dish from/to the shelf. Also, your shelf class makes the cook/waitress wait on different conditions.
So, you are doing what you want. Just increase the sleep interval for cook and you will see waitresses waiting.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!