By puting the "if -statement" inside the run() method, threads get created in order. If a change the "if-statement" to a "while" (same boolean condition), the Threads get created randomly, but it doesn't pass the testing. I'm confused. What is the difference?
Thanks!
public class Solution {
static int count = 15;
static volatile int createdThreadCount;
public static void main(String[] args) {
System.out.println(new GenerateThread());
}
public static class GenerateThread extends Thread {
public GenerateThread(){
super(String.valueOf(++createdThreadCount));
start();
}
@Override
public void run() {
if(createdThreadCount < count){
GenerateThread thread = new GenerateThread();
System.out.println(thread);
}
}
@Override
public String toString() {
return getName() + " " + "created";
}
}
}