I hate multithreading, it makes me feel like I have learned nothing so far in Java.
Why can't I use :
SleepingThread.currentThread.join() // or
Thread.currentThread.join()
public class Solution {
public volatile static int COUNT = 4;
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < COUNT; i++) {
new SleepingThread().join();
//write your code here
}
}
public static class SleepingThread extends Thread {
private static volatile int threadCount = 0;
private volatile int countdownIndex = COUNT;
public SleepingThread() {
super(String.valueOf(++threadCount));
start();
}
public void run() {
while (true) {
System.out.println(this);
if (--countdownIndex == 0) return;
//write your code here
try {Thread.sleep(10);}catch (InterruptedException e){
System.out.println("Thread interrupted");
//return;
}
}
}
public String toString() {
return "#" + getName() + ": " + countdownIndex;
}
}
}