I tried many ways from former questions in this topic, and I don't know, maybe something wrong with creating threads in Solution class, or inheritance?
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 showWarning method is called, the thread should stop;
1.5. Thread 5 should read numbers from the console until "N" is entered. Then it should display the sum of the entered numbers.
2. In a static block, add your threads to List<Thread> threads in the specified order.
3. The threads should not start automatically.
Hint: Thread4can be checked using isAlive()
Requirements:
The Solution class's static block must create 5 threads and add them to the threads list.
The threads in the threads list should not start automatically.
Thread 1 in the threads list must run infinitely.
Thread 2 in the threads list should display "InterruptedException" when an InterruptedException occurs.
Thread 3 in the threads list should display "Hurray" every half second.
Thread 4 in the threads list must implement the Message interface. When the showWarning method is called, the thread should stop.
Thread 5 in the threads list should read numbers from the console until "N" is entered. Then it should display the sum of the entered numbers.
package com.codegym.task.task16.task1632;
public interface Message {
void showWarning();
}
for thread 2, I'd just use an infinity loop using true instead (that's what I actually did in my solution ;) ). Your original one has a minimal possibility that sending an interrupt is not causing the exception (that's when the task gets interrupted exactly when your thread is testing the while condition - In that case the sleep method won't throw the exception and the task ends regularly).