public class SousThread implements Runnable {
private int i;
public SousThread() {
this.i = 0;
}
@Override
public void run() {
while(true) {
System.out.println("I'm sousthread " + i++);
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class TestThread implements Runnable {
@Override
public void run() {
System.out.println("I'm thread");
SousThread st = new SousThread();
Thread td = new Thread(st);
td.start();
System.out.println("thread finished");
}
public static void main(String[] args) {
TestThread tt = new TestThread();
Thread td = new Thread(tt);
td.start();
System.out.println("main finished");
}
}
I'm trying to create thread SousThread
in thread TestThread
. SousThread
is infinite.
To my surprise, I get the result below:
I'm thread
main finished
thread finished
I'm sousthread 0
I'm sousthread 1
I'm sousthread 2
I'm sousthread 3
I'm sousthread 4
I'm sousthread 5
...
...
Does it mean that the method main
has finished whereas the thread hasn't finished yet? Is this possible? Is this safe? If not, what is the better way to do?
UPDATE
Because the code like this doesn't work in C++. So I just want to know if the code works without any problem or any risk in Java.