Any idea what is wrong with the code. I tried to change the 4th thread in every way with this.stop(), this.interrupt(), stop(), interrupt(), etc. Need some help please.
package com.codegym.task.task16.task1632;
import javax.swing.plaf.TableHeaderUI;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
1. The Solution class's static block must create 5 threads and add them to the threads list.
2. The threads in the threads list should not start automatically.
3. Thread 1 in the threads list must run infinitely.
4. Thread 2 in the threads list should display "InterruptedException" when an InterruptedException occurs.
5. Thread 3 in the threads list should display "Hurray" every half second.
6. Thread 4 in the threads list must implement the Message interface.
When the showWarning method is called, the thread should stop.
7. 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.
*/
public class Solution {
public static List<Thread> threads = new ArrayList<>(5);
static{
TestThread1 t1 = new TestThread1();
TestThread2 t2 = new TestThread2();
TestThread3 t3 = new TestThread3();
TestThread4 t4 = new TestThread4();
TestThread5 t5 = new TestThread5();
Thread thread1 = new Thread(t1);
Thread thread2 = new Thread(t2);
Thread thread3 = new Thread(t3);
Thread thread4 = new Thread(t4);
Thread thread5 = new Thread(t5);
threads.add(thread1);threads.add(thread2);threads.add(thread3);threads.add(thread4);threads.add(thread5);
}
public static void main(String[] args) {
for(Thread t : threads){
t.start();
}
}
public static class TestThread1 extends Thread {
public void run() {
while (true) {
}
}
}
public static class TestThread2 extends Thread{
public void run() {
try {
throw new InterruptedException();
}catch (InterruptedException e){
System.out.println("InterruptedException");
}
}
}
public static class TestThread3 extends Thread{
public void run(){
try {
while(!isInterrupted()) {
System.out.println("Hurray");
Thread.sleep(500);
}
}catch (InterruptedException e){
}
}
}
public static class TestThread4 extends Thread implements Message{
@Override
public void showWarning() {
stop();
}
@Override
public void run() {
while (isAlive()) {
}
}
}
public static class TestThread5 extends Thread {
int sum = 0;
public void run() {
try {
while (true) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input = reader.readLine();
if (input.equals("N")){
break;
}
sum = Integer.parseInt(input) + sum;
System.out.println(sum);
}
} catch (IOException e) {
}
}
}
}