Dogs Race case
This exercise is intended for the student to know and apply concepts of concurrent programming.
Last updated
This exercise is intended for the student to know and apply concepts of concurrent programming.
Last updated
Creation, commissioning and coordination of threads.
Review the "concurrent cousins" program (in the folder part1), provided in the package edu.eci.arsw.primefinder
. This is a program that calculates the prime numbers between two intervals, distributing their search among independent threads. For now, it has a single thread that seeks cousins between 0 and 30,000,000. Run it, open the operating system process manager, and verify how many cores are used by it.
Modify the program so that, instead of solving the problem with a single thread, do it with three, where each of these will make up the first part of the original problem. Check the operation again, and again check the use of the equipment cores.
What you have been asked for is: you must modify the application so that when 5 seconds have elapsed since the execution started, all the threads are stopped and the number of primes found so far is displayed. Then, you must wait for the user to press ENTER to resume their execution.
For this exercise, you will work with a greyhound racing simulator (folder part2), whose graphic representation corresponds to the following figure:
In the simulation, all the greyhounds have the same speed (at the programming level), so the winning greyhound will be the one that (for reasons of chance) has been most benefited by the scheduling of the processor (that is, the one with the most cycles CPU has been granted during the race). The application model is as follows:
As you can see, greyhounds are thread objects, and their progress is displayed in the Canodromo
class, which is basically a Swing
form. All greyhounds (by default, there are 17 greyhounds running on a 100-meter track) share access to an object of type RegistrationLlegada
. When a greyhound reaches the goal, it accesses the counter located on said object (whose initial value is 1), and takes that value as its arrival position, and then increases it by 1. The greyhound that manages to take the '1' will be the winner.
When starting the application, there is a first obvious error: the results (total run and number of the winning greyhound) are shown before the end of the race as such. However, once this is corrected, there may be more inconsistencies caused by the presence of race conditions.
Fix the application so that the results notice is shown only when the execution of all the ‘greyhound’ threads is finished. For this keep in mind:
The action of starting the race and showing the results is carried out from line 38 of MainCanodromo
.
The join()
method of the Thread
class can be used to synchronize the thread that starts the race, with the completion of the greyhound threads.
Once the initial problem has been corrected, run the application several times, and identify the inconsistencies in the results of the same by seeing the ‘ranking’ shown on the console (sometimes valid results could appear, but in other cases such inconsistencies may occur). From this, identify the critical regions of the program.
Use a synchronization mechanism to ensure that these critical regions only access one thread at a time. Verify the results.
Implement the pause and continue functionalities. With these, when "Stop" is clicked, all the threads of the greyhounds should fall asleep, and when "Continue" is clicked they should wake up and continue with the race. Design a solution that allows you to do this using the synchronization mechanisms with the Locks primitives provided by the language (wait and notifyAll).
The deliver of this workshop should be a link to GitHub repository where the README file contains all the answers (you can use images to support your solution development) and the source code is the solution.
The README file should contains:
Compile and run instructions.
Answers to all the workshop questions.
Another grade criteria will be:
Use of Maven.
Use of GitHub.
Include your own tests.
Functionality
The execution of the greyhounds can be stopped and summarized consistently.
There are no inconsistencies in the order of arrival registered. Design.
Only the critical region is synchronized (synchronizing, for example, a whole method, would block more than necessary).
Greyhounds, when suspended, are reactivated are just a call (using a common monitor). "