We're supposed to .join() each created SleepingThread object. But this code actually does something completely different from any of the previous tasks and I'm not sure what to do in this case.
Usually when we create a new Thread or something inheriting Thread, it's using syntax sort of like
Thread t = new myThread();
t.join();
But in this task, new SleepingThread() isn't assigned to any variable. There's nothing to stick .join() to.package com.codegym.task.task16.task1622;
/*
Consecutive threads
*/
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();
//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;
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
System.out.println("Thread Interrupted");
}
}
}
public String toString()
{
return "#" + getName() + ": " + countdownIndex;
}
}
}