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( ), and maybe synchronized methods.

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

In the next story they are two types of threads: racer and judge.

The Race

A number of racers will compete in a race with three obstacles: The Forest, The Mountain and The River. Before the first obstacle and between any two obstacles, the racers will stop for rest and food (by sleeping for a random time). The sequence of events will be something similar to:

Rest
Forest
Rest
Mountain
Rest
River
Get Home

Note: you will need to keep track of the time when a racer started a specific obstacle and when it ended that obstacle, together with the time when the racer started the race until he finished the race.

The obstacles are described below:

The Forest:

You will have to use the setPriority( ), getPriority( ) and yield( ) methods.

At the entry to the forest, the racer will try to rush as much as possible (simulated by increasing its current priority by a randomly generated number between 0-4).

The racer using a compass will have to find in the forest a map that contains a magic word. The forest will be a file of more than 300 but less than 500 randomly generated words of 4 letters chosen from the set {a, b, c, d}. The length of the magic word will also be 4 letters long, containing letters from the same set {a, b, c, d}. Immediately after the search the racer will reset its priority to the default value.

If the map with the magic word is found, the racer can leave the forest right away. Otherwise the racer will have to search the entire forest and by not finding the map in the forest will be penalized (by having to yield( ) the CPU twice).

The Mountain:

The racers have to pass a very narrow passage. Only one racer can be on the passage at a given time. In the order of their arrival, the runners add themselves (get on line) on a shared vector. Each racer will have its own variable let's say ready. Ready is initialized to False. The last racer (vector size is equal to number of racers) sets the ready variable of the first racer to True. Each thread will busy wait until its ready variable becomes True. After a specific racer crosses the passage (sleep of random time), it will enable the next racer on line to cross it too.

The River:

Before the River obstacle, the racers will take a long rest (sleep of long time). They will be interrupted from sleep by the Judge. (use the isInterrupted( ) and interrupt( ) methods) Each racer will cross the river by sleeping for a random time.

Go Home:

Each racer knows the next racer in the sequence. For example, racer1 knows racer2, racer2 knows racer3 and so on (you will have to use the isAlive( ) and join( ) methods). After a particular racer finishes the last obstacle, it will check if its friend is still in the race by using the isAlive( ) method. If yes, then the racer will wait for its friend using the join( ) method, otherwise the racer goes straight home. Consider that the last created racer will just go home immediately.

Judge:

The Judge will interrupt the racers from their long rest before The River obstacle.

The judge is also responsible with presenting the order in which racers ended the race. There will be two reports:

1. First report will consider the time it took for each racer to end the entire race. (Including the resting time)

2. The second report will consider the times that it took for each racer to pass each of the obstacles. (Excluding the resting time)

Develop a multithreaded Java program simulating the Race operations.

Your program should have two types of threads:

Racer threads (default number for racers is 10)
Judge threads

The number of racers should be read as command line arguments. The default value is 10.

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.

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.