Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The objective of this project is to familiarize you with the creation and execution of threads using the Thread class methods. You should use,
The objective of this project is to familiarize you with the creation and execution of threads using the Thread class methods. You should use, when necessary, the following methods to synchronize all threads: run(), start(), currentThread(), getName(), join(), yield(), sleep(random time), is Alive(), getPriority(), setPriority(), interrupt(). The use of semaphores to synchronize threads is strictly DISALLOWED. Additionally, you are NOT PERMITTED to use any of the following: wait(), notify(), notifyAll(), the synchronized keyword (for methods or blocks), locks, and any synchronized collections or synchronization tools that were not discussed in class or mentioned here. You CAN, however, use the modifier volatile and the AtomicInteger and Atomic Boolean classes if you choose to. Directions: Synchronize the patient, doctor, and assistant type of threads in the context of the problem described below. Please refer to and read carefully the project notes, tips and guidelines before starting the project. Initial values: patient doctor assistant num_p = 20 num_d=2 num_a = 3 A VISIT TO THE DENTIST There are num_p friends. They are having a great party: dancing, drinking, eating food and candies. Unfortunately, after the party they all got toothaches and needed to go to the dentist. They all commuted in their own way (simulated by sleep of random time) to the dentist's office and became patients. The dentist's office has num_d dentists and num_a assistants. The dentists are in their personal office (simulated by a sleep of a long time). Once a patient arrives at the dentist's office, (s)he will wait for assistance (simulate waiting by busy waiting). Patients will be assisted in a FCFS order. When an assistant is available, and there is a patient waiting, (s)he will take the necessary infor- mation from the patient (simulate by sleep of random time). Once done, (s)he will bring the pa- tient into an examination room and will let the dentists know that a patient is ready for consulta- tion. The assistant will announce to the dentist that the patient is waiting by interrupting the den- tist (use interrupt(); in the body of the catch have a message that will show that a specific den- tist has been interrupted). Note: When both dentists are available, they will take turns when a patient needs assistance. Patients wait in the examination room to be seen by the dentist (busy waiting). As part of the consultation, the dentist will randomly decide if the patient needs x-rays. If yes, he will allow one of the assistants to do it (have the dentist execute yield()). If after yield(), the assistant is not done yet with the x-ray, the dentist will execute another yield). One of the available assis- tants will rush to take the x-rays. (to simulated this, for 10 ms only that assistant will increase its priority by 2, after that it will reset it back to its default value. Use getPriority(), setPriority() and sleep(10)). The dentist will conclude his consultation. Next, he will go back into his office and wait (as be- fore by sleeping) to be interrupted. Meanwhile, if another patient arrives, (s)he will be taken care by one of the available assistants. Once the consultation is done, the patient will have to wait (busy waiting) in the waiting room for a screening about what can be done to avoid cavities. For a screening to start, at least three patients should be available to watch it. Once the screening starts, newly arrived patients will have to wait for the next screening. (make sure that your output shows who is watching the screening, the number of the screening, who is waiting for the next one). Once the screening ends, the patients who watched it will leave in the order of their name. (use isAlive() and join( ). Let's say that patient2, patient6 and patient7 were watching the screening. Patient2 will leave first, patient6 next and patient7 will be the last to leave out of this group). Once all of the patients are treated, the dentists will leave (terminate). Once all the patients are done watching the presentation, the assistants will leave (terminate). 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. The main method is ran by the main thread. All other thread classes must be manually created 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). DO NOT create packages. 4. The project asks that you create different types of threads. No manual specification of each thread's activity is allowed (e.g. no Student5.go Through The Door()) 5. Add the following lines to all the threads you instantiate: public static long time = System.currentTimeMillis(); public void msg (String m) { System.out.println("["+(System.currentTimeMillis()-time)+"]"+getName()+": "+m); } It is recommended that you initialize the time at the beginning of the main method, so that it is unique to all threads. 6. There should be output messages that describe how the threads are executing. Whenever you want to print something from a thread use: msg("some message about what action is simulated"); 7. NAME YOUR THREADS. 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("Random Thread-" + id); } 8. Design an OOP program. All thread-related tasks must be specified in its respective classes, no class body should be empty. 9. thread.sleep() is not busy wait. while (expr) {..} is busy wait. Interrupting a thread should be done ONLY on the simulation mentioned in the project and not as a way of substituting busy waiting. 10. FCFS should be implemented in a queue or other data structure. 11. DO NOT USE System.exit(0); the threads are supposed to terminate naturally by running to the end of their run methods. 14. Javadoc is not required. Proper basic commenting explaining the flow of the program, self- explanatory variable names, correct whitespace and indentations are required. Headers with information about class name, last modified time, author... is mandatory for each java file. 15. Choose the appropriate amount of time(s) that will agree with the content of the story. I haven't written the code for this project yet, but from the experience of grading previous semesters' projects, a project should take somewhere between 8 seconds and 15 seconds to run and complete. Set the time in such a way so you don't create only exceptional situations. A helpful tip: -If you run into some synchronization issues, and don't know which thread or threads are causing it, press F11 (in Eclipse) which will run the program in debug mode. You will clearly see the thread names in the debug perspective.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Handling this multithreaded scenario is quite a task There are a few key components to consider Pati...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started