Distributed and Concurrent Systems 28.3.2008 Exercise 3 In the following exercises, we make a small reaction time measurement game. Even if it could be done more simply, it must be implemented using threads for exercise purposes. In examples www-page http://cs.joensuu.fi/pages/sjuva/hs.esimerkit/ there is a program skeleton, StopWatch helper class and a binary (.jar) example solution of exercise 16. The following X2 exercise is obligatory for all students. The answers to X-exercises have to be unique for every student. No copies of the same answer are allowed. The answer has to be sent by Thursday 2:00 pm (the previous day). You will receive an acknowledgment upon successful processing. Answers will be graded. The answer must also contain a short self-evaluation (as a program comment) in which you describe whether the program works, nearly works, or does not probably work; how efficient it is, etc. A correct and proper self-evaluation is worth one point (in case of a proper answer). Send your answer to using cs to user sjuva with a subject HS_X2_username and the answer (with self-evaluation) as the body of the message (no attachments). At simplest using program mail at cs: /usr/ucb/mail -s HS_X2_username sjuva < username.java Where username is your cs username and username.java is a text file containing your answer. To make program compilable, the main class name must be exactly the same as your user name. You don't have to include StopWatch class. Notice, that javac doesn't accept HTML, MIME, etc. X2) Write class (thread) DelayThread which gets (for constructor) as parameter the name (String) and delay (ms). After start, thread sleeps given delay, prints it's name, and starts a stopwatch. The class also has a method time() that returns the time elapsed since the print was done (or "not started" if it was called before the print). Implement also a method cancel() that stops waiting and cancels printing. Using the above thread class, write a small game that measures the reaction time of the user. Program prints "Ready", "Steady", and starts a DelayThread with a random delay and name "Press ENTER". After this, it waits for user to press ENTER. After user pressed, it prints the time() of the DelayThread and restarts. 16) Add to the previous game a fake printing with another thread (name "Don't press", and similar random delay). If fake print comes first, the user may not press ENTER (if (s)he presses, (s)he will be too early on real print). If the correct print comes first, fake print should not appear at all. If user presses too early, neither prints should appear (threads are canceled and/or printing is prevented). 17) Add to the previous exercise a locking so that if fake comes first, correct printing will be blocked at least for a 0.5 s. Use a lock to accomplish this, the fake acquires to lock at printing for the half second. The correct thread also must acquire the lock before print. 18) Change task 16 so that if fake print comes first, it will interrupt the correct print, and both of the threads will use a new random delay (from user's point of view, they will be restarted).