I came to know in Java that: children threads won't outlive the main thread, but, seemingly, the behavior of this application shows different results.
The child thread keeps working while the main one has done working!
Here is what I do:
public class Main {
public static void main(String[] args) {
Thread t = Thread.currentThread();
// Starting a new child thread here
new NewThread();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("\n This is the last thing in the main Thread!");
}
}
class NewThread implements Runnable {
private Thread t;
NewThread(){
t= new Thread(this,"My New Thread");
t.start();
}
public void run(){
for (int i = 0; i <40; i++) {
try {
Thread.sleep(4000);
System.out.printf("Second thread printout!\n");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
What do I fail to understand here … Or that's a new feature in Java starting from JDK 9?