Задачу решил, но так и не понял, почему :(
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) {
System.out.println(new GenerateThread());
}
}
@Override
public String toString() {
return this.getName() + " created";
}
}
}
В чем разница между:
public void run() {
if (createdThreadCount < count) {
System.out.println(new GenerateThread());
}
}
и
public void run() {
for (int i = 0; i < count; i++) {
System.out.println(new GenerateThread());
}
}
почему последний вариант дает бесконечный цикл создания нитей ?