CS 511: Homework Assignment 2

$30.00 $24.00

Problem Description You are the owner of a bakery that specializes in different types of bread. There are three shelves, one for loaves of rye bread, one for loaves of sourdough bread and one for loaves of wonder bread. At the start of the day, your store bakes 20 loaves each of rye, sourdough and…

Rate this product

You’ll get a: zip file solution

 

Categorys:

Description

Rate this product

Problem Description

You are the owner of a bakery that specializes in different types of bread. There are three shelves, one for loaves of rye bread, one for loaves of sourdough bread and one for loaves of wonder bread. At the start of the day, your store bakes 20 loaves each of rye, sourdough and wonder bread and places them on their corresponding shelves. Throughout the day customers come in and pick up the

CS511 – Concurrent Programming 3 PROBLEM DESCRIPTION

Figure 1: Bakery

loaves of bread from the shelves. They may buy more than one loaf of bread of any type, as described below. Then they make their way to one of the four cashiers that you employ. Those cashiers receive the customer’s payments and update the value of the global sales. The customers then leave the store. For safety reasons your store occupancy must be at or below 50 people.

The bakery, as describe above, is illustrated in Figure 1. The green arrow depicts the access door to the bakery and the red arrow the exit door. Only one customer can remove a loaf from a shelf at a time. Only one customer can be at a register at a time.

3.1 The Bakery

The simulation of the bakery starts by spawning one thread executing the code in the method Bakery.run(). The file Assignment2.java is in charge of doing this. Below we describe the contents of the class Bakery. In this particular class, you will have to:

  1. Complete the implementation of the method Bakery.run()

  1. Declare any necessary semaphores

The Bakery class has the following fields and methods:

CS511 – Concurrent Programming 3 PROBLEM DESCRIPTION

Regarding the simulation of the bakery, the bakery should generate customers randomly and have them do their shopping. Customers should have between one and three breads in their shopping list. The shopping itself should take some time but not too much (in the sense that the simulation doesn’t slow down too much). Also, the cashiers when checking out should take some time. The simulation should print out events when a customer starts shopping, takes an item from stock, when it buys, and and then when it finishes. Each customer can be uniquely identified using its hashCode.

3.3.1 Printing

After all customers have finished shopping, the program should print the total sales from the day before exiting using:

System.out.printf(“Total sales = %.2fn”, sales);

The formatting of any other strings printed by your solution are irrelevant. You may choose any formatting.

3.4 List of Classes

Here is a list of the classes you have to define, all included in a package called

Assignment2

  • Bakery

The simulation of the bakery consists in randomly having customers come in to the bakery. You may assume that there are a total of CAPACITY that are allowed in the bakery. The run method of Bakery should be in charge of the simulation itself. The threads corresponding to the customers should be spawned using a thread pool (as explained below).

  • BreadType

Is given in the stub and contains information about the bread and prices.

  • Customer

Has a shopping cart that is randomly generated from the requirements above. Also needs a random value for the time spent shopping and also the time spent at checkout. Contains a method fillShoppingCart that generates what the customer wants from the shelves.

  • Assignment2 – included in the stub. Starts the program.

1 /* start the s i m u l a t i o n */

  • public class A s s i g n m e n t 2 {

3

public static void

main

( String [] args ) {

4

Thread thread

= new

Thread ( new Bakery ());

  • thread . start ();

6

CS511 – Concurrent Programming 3 PROBLEM DESCRIPTION

  • try {

8 thread . join ();

  • } catch ( I n t e r r u p t e d E x c e p t i o n e ) {

10 e . p r i n t S t a c k T r a c e ();

  1. }

  1. }

  1. }

3.5 A Note on Pool Threads and the Executor Interface

The Executor interface1 or its subinterface ExecutorService (which is the one we will be using in this assignment) represents an asynchronous execution mech-anism which is capable of executing multiple tasks in the background. Such multiple tasks are referred to as thread pools. An Executor is normally used instead of explicitly creating threads for managing each of the tasks, i.e., rather than invoking new Thread(new(RunnableTask())).start() for each of a set of tasks, you might use:

  • Executor executor = a n E x e c u t o r ;

  • executor . execute ( new R u n n a b l e T a s k 1 ());

  • executor . execute ( new R u n n a b l e T a s k 2 ());

Thread pools are useful when we need to limit the number of threads running in an application at the same time as there is a performance overhead associated with starting a new thread (each thread is also allocated some memory for its stack, etcetera). Instead of starting a new thread for every task to execute concurrently, the task can be passed to a thread pool. As soon as the pool has any idle threads the task is assigned to one of them and is then executed.

There are many ways of managing thread pools. One easy way is to use the static method newFixedThreadPool of the class Executors (not to be confused with the Executor interface). This method, whose signature is

public static ExecutorService newFixedThreadPool(int nThreads)

creates a thread pool that reuses a fixed number of threads operating off a shared (unbounded) queue. At any point, at most n threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.

Here is a simple example of ExecutorService:

1 E x e c u t o r S e r v i c e e x e c u t o r S e r v i c e = E xec ut or s . n e w F i x e d T h r e a d P o o l (10);

2

  • e x e c u t o r S e r v i c e . execute ( new Runnable () { 1Found in java.util.concurrent

CS511 – Concurrent Programming 3 PROBLEM DESCRIPTION

  • public void run () {

5 System . out . println (“I am an a s y n c h r o n o u s task !” );

  • }

  • });

8

  • e x e c u t o r S e r v i c e . shutdown ();

Here is what is happening:

    • An ExecutorService is created using the newFixedThreadPool(int) factory method. This creates a thread pool with 10 threads executing tasks.

    • An anonymous class implementation of the Runnable interface is passed to the execute() method. This causes the Runnable to be executed by one of the threads in the ExecutorService.

    • The ExecutorService is shut down, so the all threads finish running. To terminate the threads inside the ExecutorService you call its shutdown() method. The ExecutorService will not shut down immediately, but it will no longer accept new tasks, and once all threads have finished current tasks, it shuts down. All tasks submitted to the ExecutorService before shutdown() is called, are executed.

If you want to shut down the ExecutorService immediately, you can call the shutdownNow() method. This will attempt to stop all executing tasks right away, and skips all submitted but non-processed tasks. There are no guarantees given about the executing tasks. Perhaps they stop, perhaps the execute until the end. It is a best effort attempt.

3.5.1 Waiting for your threads to finish

After all customers have finished shopping, the program should print the total sales from the day before exiting. In order to wait for all threads in the thread pool to finish, we use a count down latch2 and issue the shutdown after they have all terminated. The latch itself is declared in the Bakery class:

1 public class Bakery i m p l e m e n t s Runnable {

3 private C o u n t D o w n L a t c h d o n e S i g n a l = new C o u n t D o w n L a t c h ( T O T A L _ C U S T O M E R S );

  • }

We Bakery.run() then ends with the following code:

  • try {

2

d o n e S i g n a l . await (); //

wait for

all to finish

3

System . out . printf (” Total

sales =

%.2f\n” , sales );

CountDownLatch.html

CS511 – Concurrent Programming 4 SUBMISSION INSTRUCTIONS

8

CS 511: Homework Assignment 2
$30.00 $24.00