The early bird gets the worm

  • 18
  • Locked
1. Figure out what the program does. 1.1. Each thread should read lines from the console. Use the existing static BufferedReader reader. 1.2. Use AtomicInteger readStringCount to calculate how many lines all the threads have read from the console. 2. Implement the run method: 2.1. As long as a thr
You can't complete this task, because you're not signed in.
Comments (25)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Vadim “迪姆哥”
Level 26 , Kazakhstan
29 January, 13:35
I found one issue in correct code: After "interrupt" signal was sent, and reader.close() also executes, main thread finish it's execution, but few children threads freezes in "waiting data from BufferedReader" state, cuz them enters readLine right before interrupt signal was arrived. This causes main thread hands forever, until user enters few more lines, to let children threads process the interrupt then finish execution, only after that main thread will be closed with code 0. But I think the right way how it must to works is: after each thread prints out .toString(), program must be closed immediately. To archive that, we need to add one check right into while(!isInterrupted()), to achieve each thread non-blocking read behavior:
while (!reader.ready()){
Jaime Padilla
Level 22 , Chandler, United States
25 October 2022, 09:35
I had to change if (!string.isempty) to (string != null) to pass. It may not have any bugs, but some information that lines cannot be null would be nice to see in the conditions.
Jonaskinny Java Developer at Sandmedia
19 April 2022, 20:01
Task is not broken, and it does not run forever as long as you remember that the often underused 'return' statement can be used to exit a method once a condition has been met. So in the case of run() we could have IOException, and when we do get that, we can just return.
Justin Smith
Level 39 , Greenfield, USA, United States
15 September 2021, 17:17
I'm not sure why this task (especially as a Hard-rated task) doesn't also have us write code to make sure the program ends after displaying the results. Seems like bad form to have the program just hang there waiting for more inputs that serve no purpose. Plus, if you actually did write code that would do this, the validation would fail, despite doing everything requested.
Level 24 , Wittenberg, Germany
5 June 2021, 14:08
task1628 (The early bird gets the worm) undocumented requirement: the validation code seems to require that all threads continue reading even after the indicated number of words has been read. Translated to testing in IntelliJ this means that you have to hit Enter three additional times after having entered the correct number of word lines. If your code stops reading after the required number of word lines has been read then you will get failed on requirements 3 and 4 (and possibly 5 as well), without any indication what might be wrong, and even if your code clearly fulfills all these requirements.
Level 16 , Frankfurt, Germany
26 April 2021, 12:38
while (count > readStringCount.get()) { } Why there is an empty while loop here?
Gellert Varga
Level 23 , Szekesfehervar, Hungary
2 September 2021, 11:30
(I inserted a variable that counts how many times this empty cycle runs. For me it was between 0 and 27,000, different for each run.) - The purpose of this cycle is purely to stall time (wait). To prevent the main thread from continuing to execute lines of code until the other threads have increased the value of the readStringCount common variable by the required amount. In other words, to allow main to execute the interrupt command only after the other threads have finished their read task.
Guadalupe Gagnon
Level 37 , Tampa, United States
10 March 2021, 18:31
There is no bugs in this task. The reason that if "fails" to stop running is because interrupt() does not stop a thread. All it does is set an internal boolean to true that you use for the while loop condition. The reason it is continuing in each case is because it is waiting for user input on the reader.readlin() method, which is unaffected by the "interrupt". When all those lines finish reading data, and the while loop is broken, then the program will complete. Also, reader.readline() can return null, which means nothing was read. This should not be put in the ArrayList of items that were read - if nothing was read then nothing should be added
Jurij Thmsn
Level 29 , Flensburg, Germany
23 March 2021, 16:14
Thanks for this tip! This fault is hard to detect when using IntelliJ, because in contrary to the browser-compiler, IntelliJ doesn't seem to display the "null"-entries so the output doesn't change with the null-check.
Gellert Varga
Level 23 , Szekesfehervar, Hungary
16 April 2021, 20:45
Thanks for the tip! But this was again a case where the CG was unable to clearly list all the requirements to be met.
Oliver Heintz
Level 18 , Mustang, United States
8 February 2021, 18:06
I came to the comments to see how to stop my program from running, as I assumed that this would cause me to fail verification. Instead, I read that I needed to add an if statement to check if input was null. Thanks, guys! I passed on the first try, even if my program never stops running!
4 February 2021, 00:00
This one would not be too difficult to pass if we did not have to ensure that Strings that follow after "count" variable should be tested for NULL value. CodeGym please fix this or add this as requirement.
Liliane Top Backend Developer at Procura
26 December 2020, 19:31
Ik think there is a bug in this task. I copied the answer but it had the same problem. It passes the test but fails to stop the programm It just keeps running.