I have written a basic code in Thread and the output which i got is pretty surprising.
public class ThreadImp implements Runnable{
public static void main(String[] args) {
ThreadImp threadImp = new ThreadImp();
Thread t =new Thread(threadImp);
t.setName("Fred");
t.start();
threadImp.run();
t.run();
}
public void run(){
System.out.println("Current Thread: "+ Thread.currentThread());
}
}
The output here i am expecting is that it will be print
Current Thread: Thread[main,5,main]
Current Thread: Thread[Fred,5,main]
Current Thread: Thread[main,5,main]
This result i can understand, that there is only one thread which i am creating i.e Thread Fred. Other two calls to run method is just like calling a normal method.
But what i can't understand is sometimes when i execute the same program i am getting the following output. So can anyone explain it to me why is it behaving like this.
Current Thread: Thread[main,5,main]
Current Thread: Thread[Fred,5,main]