I know that when we call run()
explicitly no new thread is started and the code written inside run()
is executed on the main thread. But what if I give a name to a thread and then call run()
explicitly as shown in my code?
public class ThreadImpl1 extends Thread{
ThreadImpl1(String name){
super(name);
//start();
}
public void run() {
for(int i=0;i<5;i++)
System.out.println(getName());
}
public static void main(String...s) {
ThreadImpl1 t1=new ThreadImpl1("thread1");
t1.run();
for(int i=0;i<5;i++)
System.out.println(Thread.currentThread().getName());
}
}
It gives the following output:
thread1
thread1
thread1
thread1
thread1
main
main
main
main
main
Why does the output show its name i.e. thread1? I haven't started the thread yet so it should print "main" all the time.