Skip to main content

The Race Java Multi-Threading project with complete source code

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:

  1. Rest
  2. Forest
  3. Rest
  4. Mountain
  5. Rest
  6. River
  7. 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:


  1. Racer threads (default number for racers is 10)
  2. 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.
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 Project’s 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. Don’t 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 Racer1.goOverPassage())
  5. Add the following lines to all the threads you make: public static long time = System.currentTimeMillis();
    1. public void msg(String m) { System.out.println("["+(System.currentTimeMillis()-time)+"] "+getName()+": "+m); }
    2. 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):
    1. // 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. A command line argument must be implemented to allow changes to the nRacer variable.
  14. Javadoc is not required. Proper basic commenting explaining the flow of the program, self-explanatory variable names, correct whitespace and indentations are required.

Download your project now

Buy now

Comments

Popular posts from this blog

The Zoo Management System - entity relationship diagram & MS Access Database

Zoo Management System - Project Details: You are the employee of a big, worldwide working Zoo Management Company. Your company is responsible for the Zoo management. Your boss thinks it would be a great idea to store all data for each Zoo in a brand new self-developed ZOO Management System. Up to now, the ZOO management company has maps of each ZOO available. Your boss knows that you took a course in introduction on an ERP system, so he asks you if you could help designing such a system. Each ZOO must have the same organizational structure, which should look like this: Each Zoo has a Zoo-Address. Each Zoo has many visitors (Visitor Ticket Process (VTP). Many Zoo-Attractions belong to a Zoo. Module 1: Entity Relationship Diagram Design a ER (entity-relationship) diagram for your ZOO Management System. Use the information provided below with the entities and its attributes. Put the entities in the correct relationship to each other (organizational structure). Module 2: DB Implem...

EIT Knowledge and Innovative Community Scholarships has been announced

Admission Criteria To qualify for our programmes, applicants need to fulfill the admission requirements based on previous studies, English proficiency and relevant documentation. Previous Studies: A Completed Bachelor’s Degree In order to be admitted into a KIC InnoEnergy MSc programme, you must have completed a Bachelor’s degree encompassing a minimum of 180 ECTS credits or equivalent academic qualifications from an internationally recognized university. Please note that admissions depend on the specific BSc degree you hold for entry into the MSc programme you are interested in. Conditional Acceptance – Undergraduate Students in Final Year Students in their final year of undergraduate education may also apply and if expected to qualify, receive a conditional offer. If you have not completed your studies, please include a written statement from your university’s administration office (or equivalent department), confirming that you are enrolled in the final year of your study programme ...

Human Physiology by Stuart Ira Fox [PDF] (12th edition) free download