• 18
  • Locked
1. Create 5 different threads that differ from Thread: 1.1. Thread 1 must run indefinitely; 1.2. Thread 2 should display "InterruptedException" when an InterruptedException occurs; 1.3. Thread 3 should display "Hurray" every half second"; 1.4. Thread 4 must implement the Message interface. When the
You can't complete this task, because you're not signed in.
Comments (12)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Jonaskinny Java Developer at Sandmedia
25 April 2022, 02:47
Passed using slighly different solution, try with resources, and different way of using exception on Thread5, so I'm assuming the task may have been improved? Using anonymous inner class declarations passing anonymous Runnable declarations hits a limit on Thread 4, so that was a good lesson to learn the hard way (by implementing 1-4). Even though we are tought to favor implementing Runnable, the wording does say Thread, and in the case of Thread 4 its required.
Олег Байбула
Level 27 , Ukraine
27 January, 05:52
In the requirements was said the next "The Solution class's static block must create 5 threads". So I got stuck with Thread4's implementation.
4 February 2021, 17:18
And another confusing task from CodeGym. "When the showWarning method is called, the thread should stop" to me (and most judging by the help sections) it means that I must call showWarning method to interrupt/stop said Thread. EXCEPT THAT IS DOES NOT MEAN THAT AT ALL! To all: Do not call showWarning in run method in thread 4.
Jurij Thmsn
Level 29 , Flensburg, Germany
25 March 2021, 15:20
I think exactly this was the lesson - as stated earlier in an article "you can't stop a thread, it can only stop itself". Very confusing nevertheless.
Level 24 , Wittenberg, Germany
5 June 2021, 18:27
I think that the requirements for this brilliant task are formulated well enough to be implemented straight from the sheet, without any necessity for trial and error. The requirement for thread 2 might leave a tiny bit of room for doubt whether the thread should display its text whenever an InterruptedException occurs (i.e. potentially more than once) or whether only a single 'firing' is to be expected. However, the use of 'when' instead of 'whenever' strongly suggests the single-fire case, and the repeat-fire case would go against the standard expectation that a thread should effectively be stopped when it is interrupted. As regards the stopping of thread 4 - I read that requirement as 'do the usual thing that makes a thread stop', which boils down to calling the function we've been using umpteen times to put our threads out of their mysery. It never crossed my mind to use the stop() function because that is a holdover from prehistoric times and it has been deprecated for ages. Thread 4 is a bit the odd one out in another regard: all the other tasks can be done just as well with runnables given to vanilla threads as they can with custom threads. For thread 4 that is not the case, for obvious reasons. However, I did put my foot squarely in that trap, and I paid for that brain fart with a failed first submission. Oh well, a bit of egg on one's face every now and then is supposed to be good for the soul and a great catalyst for learning ... 😉
11 August 2020, 14:08
Need to form a habit to use equals for Strings comparison!
Level 17 , Stockholm, Sweden
20 March 2020, 14:47
You can't use interrupted() for thread4, you have to use the deprecated stop(). Was that intentional or just because the tasks were written too long ago?
Level 19
11 April 2020, 20:37
It works fine with interrupt(): showWarning() has to interrupt the thread and while !(isInterrupted) thread4 keeps running.
Level 41 , São Paulo, Brazil
12 June 2020, 14:03
Yeah, I could only pass this one by using stop() instead of interrupt().
Level 24 , Bremen, Germany
8 March 2020, 16:44
Even it is not clearly stated: Thread3 must first display and then sleep hope it helps someone :)
Justin Smith
Level 37 , Greenfield, USA, United States
17 September 2021, 18:51
I find there have been multiple times in these tasks where the conditions aren't clear if they want you to sleep first, then do other work, or the other way around.
Nyerges Ferenc
Level 23 , Bükkábrány, Hungary
14 February 2020, 18:25
Cool task, brings together many things. I'm happy for making it at first attempt :-)