Mein Output ist wie gewünscht 15 Zeilen:
1 erstellt
2 erstellt
3 erstellt
4 erstellt
5 erstellt
7 erstellt
6 erstellt
12 erstellt
15 erstellt
14 erstellt
11 erstellt
13 erstellt
10 erstellt
9 erstellt
8 erstellt
Trotzdem sagt er zu viel Ausgabe auf dem Screen. Kann mir bitte einer erklären warum?
package de.codegym.task.task16.task1623;
/*
Threads rekursiv erzeugen
*/
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 += 1));
start();
}
public void run() {
while (true) {
if (createdThreadCount < Solution.count) {
GenerateThread neu = new GenerateThread();
System.out.println(neu);}
else return;
}
}
public String toString() {
return this.getName() + " erstellt";
}
}
}