user10604073
user10604073

Reputation:

It it hard to figure it out myself that how current thread works in this code worked

class MyThread extends Thread{ //
    public void run(){
        Thread t1=Thread.currentThread();
        System.out.println(t1.getName());
    }
 }
 class Demo{
    public static void main(String args[]){
        Thread t1=new MyThread();
        t1.setName("MyThread");
        t1.run();
        t1.start();
    }
 }

It prints "main" when it calls run().Why it is not the "MyThread"

Upvotes: 0

Views: 43

Answers (1)

Alon
Alon

Reputation: 695

It is not the MyThread class because run() method is just call, it is still running in the context of the main thread.

A thread itself does not turn into a new thread until call to start() which happens after the call to run().

Upvotes: 2

Related Questions