I have a java thread implementation as follows:
class MyThread extends Thread {
private static Integer counter = 1;
public MyThread(final String name) {
super(name + "_" + counter);
counter++;
}
@Override
public void run() {
try {
sleep(0,2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName() + " true = " + true);
}
}
And the main class as follows:
public class ThreadingTest {
public static void main(String[] args) {
MyThread thisThread = new MyThread("MyThread");
thisThread.setDaemon(true);
thisThread.start();
try {
Thread.sleep(0,1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
However even though the thread is sleeping for 2 nanoseconds and the calling thread is sleeping only for 1 nano second, why is my SOUT printing the statement?
Considering the fact that timeout of the called thread is more than the calling thread, shouldn't the calling thread be terminated before it can print the statement?