Through its implementation, this project will familiarize you with the creation and execution of threads, and with the use of the Thread class methods. In order to synchronize the threads you will have to use (when necessary), run( ), start( ), currentThread( ), getName( ), join( ), yield( ), sleep(time), isAlive( ), getPriority( ), setPriority( ), interrupt( ), isInterrupted( ), maybe synchronized methods.

In synchronizing threads, DO NOT use any semaphores. DO NOT use wait( ), notify( ) or notifyAll();

A customer walks into a BALA showroom and browses around the store, for a while, before (s)he finds an item (s)he likes. When a customer does so, (s)he must go to the floor-clerks and receive a slip with which (s)he can pay for the item and then take it home.

Floor clerks wait (by doing busy waiting) for customers to arrive; then, they help them with whatever information they need. However, floor clerks can only help one customer at a time; therefore, a customer must wait in line for an available clerk to help him/her. Customers are helped on a first-come first-serve order.

(to implement a FCFS order, you can use java.util.Vector which is already synchronized, therefore you do not have to worry about synchronizing access to it).

After all of the customers are assisted, the floor clerks will take a long rest (simulated by sleep for a long time) just to be interrupted later on, when the store closes.

Once a customer gets the information needed from the floor clerk, (s)he will rush to the cashier to pay for the item. To do so, (s)he will increase his/her priority (use getPriority( ), setPriority( ) change the priority). Once the customer is at the cashier, the customer will reset his/her priority back to the default value. (you will have something like increase priority, sleep of random time that will simulate the process of paying, decrease priority)

Once the item is paid, if the item is a heavy item (decide this by generating a random number; 60% of time the items should be light weight items) the customer will have to pick- up the item from the storage room. Before proceeding to the storage room, (s)he will get hungry, so (s)he will decide to take a break first. (simulate this by using yield twice and sleep of random time). Once arrived at the storage room, the customer will pick a next number and busy wait to be called. Note: picking the number should be done in a mutual exclusion fashion. Either you declare the variable as volatile, or have it updated from inside of a synchronized method.

Initially there are n_storageClerks available in the storage room. They can help multiple customers at the same time (meaning that while one clerk is busy with a customer, another clerk can help a different customer). If the item is very heavy (decide this by generating another random number), two storage clerks will be needed to bring the item out to the customer, otherwise only one storage clerk will need to help the customer. Make sure that you simulate all these actions by using sleep of random time.

After all of the customers are assisted, the storage clerks wait for closing time (simulated by sleep of a long time) just to be interrupted later on, when the store closes.

Once the shopping is completed, each customer will join another customer; they will leave in sequential order. Customer N joins with customer N-1, customer N-1 joins with N-2, , customer 2 joins with customer 1 (use join( ), isAlive( )).

Customer 1 will not join with any other customer; before (s)he leaves, (s)he will announce to the floor-clerks that it is closing time by interrupting them (use interrupt() and isInterrupted() method).

Note: to check that you correctly implemented this part, have the println inside the Catch block.

More about interrupting threads: Method Thread.interrupt() does not interrupt a running thread (you will use interrupt directly on your clerk threads, as in clerk1.interrupt()). What the method actually does is throw an interrupt if the thread is blocked, so that it exits the blocked state. More precisely, if the thread is not blocked, calling interrupt() will not hurt; otherwise (if the thread is blocked at one of the methods), the thread will get an exception (the thread must be prepared to handle this condition which in this project just means that you can proceed forward) and escape the blocked state.

Develop a multithreaded Java program simulating the Shopping at BALA operations.

Your program should have three types of threads:

Customer threads
Floor clerk threads
Storage clerk threads

The number of customers, floor clerks and storage clerks should be read as command line arguments: e.g. c < int> -f < int> -s < int> with default values of 18, 2 and 4 respectively. In order to simulate different actions you must pick reasonable intervals of random time. Make sure that the execution of the entire program is somewhere between 40 seconds and 90 seconds.

Guidelines

1. Do not submit any code that does not compile and run. If there are parts of the code that contain bugs, comment it out and leave the code in. A program that does not compile nor run will not be graded.

2. Closely follow all the requirements of the Projects description.

3. Main class is run by the main thread. The other threads must be manually specified by either implementing the Runnable interface or extending the Thread class. Separate the classes into separate files. Do not leave all the classes in one file. Create a class for each type of thread. Dont create packages.

4. The program asks you to create different types of threads. There is more than one instance of the thread. No manual specification of each thread's activity is allowed (e.g. no Clerk1.checkCustomer()).

5. Add the following lines to all the threads you make:

public static long time = System.currentTimeMillis();
public void msg(String m) {
System.out.println("["+(System.currentTimeMillis()-time)+"] "+getName()+": "+m);
}

It is recommended to initialize time at the beginning of the main method, so that it will be unique to all threads.

6. There should be printout messages indicating the execution interleaving. Whenever you want to print something from that thread use: msg("some message about what action is simulated");

7. NAME YOUR THREADS or the above lines that were added would mean nothing. Here's how the constructors could look like (you may use any variant of this as long as each thread is unique and distinguishable):

// Default constructor
public RandomThread(int id) {
setName("RandomThread-" + id);
}

8. Design an OOP program. All thread-related tasks must be specified in its respective classes, no class body should be empty.

9. No implementation of semaphores or use of wait( ), notify( ) or notifyAll( ) are allowed.

10. thread.sleep() is not busy wait. while (expr) {..} is busy wait.

11. "Synchronized" is not a FCFS implementation. The Synchronized keyword in Java allows a lock on the method, any thread that accesses the lock first will control that block of code; it is used to enforce mutual exclusion on the critical section. FCFS should be implemented in a queue or other data structure.

12. DO NOT USE System.exit(0); the threads are supposed to terminate naturally by running to the end of their run methods.

13. Command line arguments must be implemented to allow changes to the nCustomer, nFloorClerk and nStorageClerk variables.

14. Javadoc is not required. Proper basic commenting explaining the flow of the program, self- explanatory variable names, correct whitespace and indentations are required.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.