I have tried the toString() return statement with and without the additional quotes. The program accurately displays 15 statements on the console, sequentially as well, due to the usage of join(). Yet, the code tester says that the program displays too much on the console.
Could anyone tell as to why the code fails to clear testing in this implementation?
package com.codegym.task.task16.task1623;
/*
Creating threads recursively
*/
public class Solution {
static int count = 15;
static volatile int countCreatedThreads;
public static void main(String[] args) {
new GenerateThread();
}
public static class GenerateThread extends Thread
{
public GenerateThread()
{
super(Integer.toString(++countCreatedThreads));
start();
}
@Override
public String toString() {
return "\"" + Thread.currentThread().getName() + " created" + "\"";
}
@Override
public void run() {
while (countCreatedThreads < count)
{
Thread th = new GenerateThread();
System.out.println(th);
try
{
th.join();
} catch (InterruptedException e)
{
}
}
if (countCreatedThreads == count)
{
Thread th = new GenerateThread();
System.out.println(th);
}
}
}
}