CodeGym /Courses /Java Core /Bonus task | Lesson 13 | Level 6

Bonus task | Lesson 13 | Level 6

Java Core
Level 6 , Lesson 13
Available

"Hey, soldier!"

"Good day, Captain!"

"I've got awesome news for you. Here are some exercises to reinforce your skills. Do them every day and your skills will grow at an insane speed. They were created especially for IntelliJ IDEA."

4
Task
Java Core, level 6, lesson 13
Locked
Thread.currentThread always returns the current thread
1. In the printMsg method, assign the current thread to the variable t. 2. In the printMsg method, add a delay of 1 millisecond after all the actions.
4
Task
Java Core, level 6, lesson 13
Locked
Consecutive threads
1. In the run method, add a delay of 10 milliseconds after all the actions. Display "Thread interrupted" if the thread is interrupted. 2. Make all the threads run sequentially: first, for thread 1, count down from COUNT to 1; then for thread 2 - from COUNT to 1, etc. Example: #1: 4 #1: 3 ... #1: 1 #
18
Task
Java Core, level 6, lesson 13
Locked
Creating threads recursively
1. Change the GenerateThread class to make it a thread. 2. Create a GenerateThread constructor, which must: 2.1. Call the superclass's constructor, passing the created thread's number as a String. Use createdThreadCount. 2.2. Start the current thread. 2.3. The thread numbers must start with 1. 3. O
4
Task
Java Core, level 6, lesson 13
Locked
Consecutive threads
1. Figure out what the program does. 2. Make the program first display the result of the thread, and when the thread has finished then the main method continues. 3. Example output: inside MyThread 0 inside MyThread 1 ... inside MyThread 9 inside main 0 inside main 1 ... inside main 9
4
Task
Java Core, level 6, lesson 13
Locked
Deadlock
1. Figure out how the program works. 2. Without changing the T1 and T2 classes, make it so their threads finish (not necessarily successfully). 3. Don't use the sleep method.
9
Task
Java Core, level 6, lesson 13
Locked
Following a pattern
Figure out how the program works. Similar to CountdownRunnable, create a CountUpRunnable thread that displays values ​​in the counting order: from 1 to number.
18
Task
Java Core, level 6, lesson 13
Locked
Shall we play?
Three people play the game. Each player (Gamer) is characterized by two parameters: last name (name) and the actions-per-second (rating). Display the actions taken and determine the winner and the losers. Let's begin... 1. Figure out what the program does. 1.1. List actions stores the sequen
18
Task
Java Core, level 6, lesson 13
Locked
The early bird gets the worm
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
9
Task
Java Core, level 6, lesson 13
Locked
Take your turn!
1. In the Solution class, create a public static Read3Strings class that inherits Thread. 2. In the run method, read three lines from the console. 3. Three lines should be read in succession on one thread and combined into one space-delimited string. 4. In the main method, display the result for eac
18
Task
Java Core, level 6, lesson 13
Locked
Sequential file output
1. Figure out what the program does. 2. In a static block, read 2 filenames: firstFileName and secondFileName. 3. Inside the Solution class, create a public static ReadFileThread class that implements the ReadFileInterface interface (Think about what is more appropriate: Thread or Runnable). 3.1. Th

"Those previous exercises were for rookies. I've added some more advanced bonus exercises for the old-timers. Just for veterans."

18
Task
Java Core, level 6, lesson 13
Locked
Factory method pattern
1. Take a careful look at the classes you have. 2. In separate files in the common package, create JpgReader, PngReader, and BmpReader classes that implement the ImageReader interface. 3. In a separate file in the main package, create the ImageReaderFactory class with one method. 3.1. Think about wh
18
Task
Java Core, level 6, lesson 13
Locked
Clew
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
18
Task
Java Core, level 6, lesson 13
Locked
Debug everything under the sun
Figure out what the program does. Read about UncaughtExceptionHandler - it's important. Take another careful look at the program. Figure out why our OurUncaughtExceptionHandler doesn't work (use the debugger). Fix the bug, i.e. everything should work. :) Expected result in no particular order: Thre
Comments (33)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Hoist Level 2, San Diego, United States
17 September 2024
Output: Thread 1: My exception message Thread 2: My exception message
Daniel Ketcheson Level 28, Canada
4 September 2023
Multi-threading is like getting bit by a radioactive spider. Things will be weird for a while, hard to adapt to. Ultimately, it's when you got your superpowers.
vhunjek Level 33, Varazdin, Croatia
16 February 2022
In Clew task, hint will just throw you out of track (Thread 4 can be checked using isAlive()).
vhunjek Level 33, Varazdin, Croatia
14 February 2022
In Creating threads recursively, why Thread.currentThread().getName() isn't validated in toString method?
vhunjek Level 33, Varazdin, Croatia
14 February 2022
In first Consecutive threads task first comment "//write your code here" is placed inappropriately.
DarthGizka Level 24, Wittenberg, Germany
5 June 2021
task1631 (Factory method pattern) pitfall: the task turned up another dark corner of the language, which is that switch statements explode if presented with a null value. This is somewhat suprising since there is no dereferencing of any pointers involved, only comparison of pointer values. A chained if statement with operator == would not exhibit such weird behaviour. Anyway, this is relevant to the task at hand because there is a specific requirement which exception should be thrown for any illegal arguments.
Anonymous #10843013 Level 24, Germany, Germany
17 May 2022
Thank you for bringing this up! I spend the last 10minutes searching for my mistake in the switch-case
DarthGizka Level 24, Wittenberg, Germany
5 June 2021
task1630 (Sequential file output) undocumented requirement: your code will be failed unless it reacts to invalid inputs by printing an empty string instead of null or whatever (i.e. the content of a non-existing file is deemed to be "").
DarthGizka Level 24, Wittenberg, Germany
5 June 2021
task1626 (Following a pattern) caveat: you will get failed if you 'follow the pattern' (i.e. if you emulate the broken logic of the CountdownRunnable code as you are told) because it runs afoul of the requirement "The CountUpRunnable class's run method should run for approximately 2.5 seconds". I bet on the wrong horse and lost - don't follow my pattern. 😉
ayhem bouabid Level 25, Tunis, Tunisia
30 May 2021
That moment when one completes all the easy and medium tasks and has to face the hard tasks...
Andrei Level 41
6 January 2021
Man, it took me maybe 1 month or so to finish this chapter. Try these 3 videos, they helped me with unhandled exceptions: 1. https://www.youtube.com/watch?v=7yM890QoSD8&list=PL1ZYhU1nPNh-ZzXrhaXhsNhrb9SlTtMJd&index=4&ab_channel=JavaJ2EEChamp 2. https://www.youtube.com/watch?v=1UOSwTfWNIc&list=PL1ZYhU1nPNh-ZzXrhaXhsNhrb9SlTtMJd&index=3&t=1s&ab_channel=JavaJ2EEChamp 3. https://www.youtube.com/watch?v=JlVPbxrVrck&list=PL1ZYhU1nPNh-ZzXrhaXhsNhrb9SlTtMJd&index=2&ab_channel=JavaJ2EEChamp
Mark Costales Level 24
22 January 2022
I watched all three videos before attempting the task. It helped a lot. Thanks