User Tools

Site Tools


lab7

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
lab7 [2018/03/11 23:18] francklab7 [2019/03/14 01:13] (current) franck
Line 1: Line 1:
 ====== Lab 7 ====== ====== Lab 7 ======
  
-In this lab, we consider concurrent stack.  We start from the following skeleton of a sequential stack.  Note that the class Stack does not implement a stack correctly. +You are asked to implement solution to the sleeping barber problem in Java.  Some Java code has already been developed (see below).  First, we describe the problem.
-<code java> +
-/** +
- * A stack.  It stores integers. +
- * +
- * @author +
- */ +
-public class Stack { +
-  private int[] content; +
-  private int size;+
  
-  /* +The barber has one barber's chair and a waiting room containing a number of chairs in it.  When the barber finishes cutting customer's hair, he dismisses the customer and goes to the waiting room to see if there are others waiting.  If there are, he brings one of them back to the chairs and cut their hair.  If there are none, he returns to the chair and sleeps in it.
-   The maximal number of integers that can be stored in a stack. +
-   */ +
-  private final static int CAPACITY = 5;+
  
-  /** +Each customer, when they arrive, looks to see what the barber is doing.  If the barber is sleeping, the customer wakes him up If the barber is cutting hair, the customer stays in the waiting room If there is a free chair in the waiting room, the customer sits in it and waits their turn.  If there is no free chair, the customer leaves.
-   * Initializes this stack to be empty. +
-   */ +
-  public Stack() { +
-    this.content = new int[Stack.CAPACITY]; +
-    this.size = 0; +
-  }+
  
-  /** +The waiting room is represented by the class WaitingRoom and the barber and the customers are represented by Barber and Customer threads.  The API of these classes can be found [[http://www.eecs.yorku.ca/course_archive/2018-19/W/4315/lab7/barber.api/index.html|here]].  Implement the classes WaitingRoom and Barber An implementation of the Customer class can be found [[http://www.eecs.yorku.ca/course_archive/2018-19/W/4315/lab7/Customer.java.txt|here]].  The sleeping barber app can be found [[http://www.eecs.yorku.ca/course_archive/2018-19/W/4315/lab7/Main.java.txt|here]].
-   * Pushes the given integer onto this stack. +
-   * +
-   * @param value the integer to be pushed onto this stack. +
-   */ +
-  public void push(int value) { +
-    this.content[this.size] = value; +
-    this.size++; +
-    System.out.printf("Push %d\n", value); +
-  }+
  
-  /** +Since randomization is used in some of the classes, different runs of the app may produce different outputs
-   * Pops the top of this stack and returns the integer+<code> 
-   * +Barber sleeps 
-   * @return the top of this stack. +Customer 4 enters 
-   */ +Customer 4 sits down 
-  public int pop() { +Barber wakes up 
-    this.size--; +Customer 4 gets a haircut 
-    int value = this.content[this.size]; +Customer 4 leaves 
-    System.out.printf("Pop %d\n", value); +Customer 3 enters 
-    return value; +Customer 3 sits down 
-  } +Customer 1 enters 
-}+Customer 1 sits down 
 +Customer 3 gets a haircut 
 +Customer 3 leaves 
 +Customer 0 enters 
 +Customer 0 sits down 
 +Customer 2 enters 
 +Customer 2 sits down 
 +Customer 1 gets a haircut 
 +Customer 1 leaves 
 +Customer 0 gets a haircut 
 +Customer 0 leaves 
 +Customer 2 gets a haircut 
 +Customer 2 leaves 
 +Barber is done for the day
 </code> </code>
 +In the above run, there are never more than three customers in the waiting room and therefore all customers get a haircut.
 +<code>
 +Barber sleeps
 +Customer 1 enters
 +Customer 1 sits down
 +Barber wakes up
 +Customer 2 enters
 +Customer 2 sits down
 +Customer 3 enters
 +Customer 3 sits down
 +Customer 4 enters
 +Customer 4 leaves because no chair is free
 +Customer 0 enters
 +Customer 0 leaves because no chair is free
 +Customer 1 gets a haircut
 +Customer 1 leaves
 +Customer 2 gets a haircut
 +Customer 2 leaves
 +Customer 3 gets a haircut
 +Customer 3 leaves
 +Barber is done for the day
 +</code>
 +In the above run, customers 4 and 0 leave before getting a haircut since all chairs in the waiting room are occupied.
  
-Implement the following classes. +To receive feedback, submit your code **before** March 23 using the submit command:\\
-  * The class Pusher.  Each Pusher object is a Thread that pushes the integers 0, 1, ..., 9 onto a stack.  The stack is provided as an argument to the constructor of the Pusher class.  The jar file [[:pop.jar|pop.jar]] contains the class Popper.  Each Popper object is a Thread that pops ten integers from a stack.  The stack is provided as an argument to the constructor of the Popper class. +
-  * The class Stack.  This class is based on the above skeleton and allows Threads to concurrently call the push and pop methods on the stack. +
-  * The class Main.  This class only contains a main method.  In the main method, a number of Popper and Pusher objects are created (for example, 10 Poppers and 5 Pushers) and all these Threads are run concurrently. +
- +
-To receive feedback, submit your code using the submit command:\\+
 <code> <code>
-submit 4315 lab7 <name of file>.java+submit 4315 lab7 <name of class>.java
 </code> </code>
  
lab7.1520810307.txt.gz · Last modified: 2018/03/11 23:18 by franck

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki