Chowdappa
Chowdappa

Reputation: 1620

Between "this" object and CurrentThread in constructor

While doing some practices on multi-threading, I found that I can not set the name of the thread in my code. I can use this to refer the current object then why I can not use Thread.currentThread while constructing thread to access current thread. I am bit confusing. please help me.

When actually thread creating? Is it when constructing thread instance or while calling method start() on thread?

what is currentThread means here?

public class RecursiveRunnableTest {

    public static void main(String... args){
        Thread t =  new Thread(new RecursiveRunnable("First thread"));
        t.start();
    }


}

class RecursiveRunnable implements Runnable {

    private String name;
    public RecursiveRunnable(String name) {
        this.name = name;
        Thread.currentThread().setName(this.name); // Expecting to set the name of thread here
    }
    @Override
    public synchronized void run() {
        System.out.println(Thread.currentThread().getName()); // displaying Thread-0
        System.out.println(this.name); // displaying "First thread"

        try{
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{

        }
    }
}

Upvotes: 0

Views: 92

Answers (2)

Yasser Zamani
Yasser Zamani

Reputation: 2500

Simply because main thread constructs it not the t thread constructs itself. So, you may rewrite to this one (setting thread's name before starting it):

public class RecursiveRunnableTest {

    public static void main(String... args){
        RecursiveRunnable rr = new RecursiveRunnable("First thread");
        Thread t =  new Thread(rr);
        t.setName(rr.getName());
        t.start();
    }


}

class RecursiveRunnable implements Runnable{

    private String name;
    public RecursiveRunnable(String name) {
        this.name = name;
    }
    public String getName(){return this.name;}
    @Override
    public synchronized void run() {
        System.out.println(Thread.currentThread().getName()); // displaying Thread-0
        System.out.println(this.name); // displaying "First thread"

        try{
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{

        }
    }
}

Upvotes: 2

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280122

Try doing this instead

Thread t = new Thread(new RecursiveRunnable("First thread"));
t.start();
Thread.sleep(1L);
System.out.println("main thread: " + Thread.currentThread().getName()); // same thread that created the RecrusiveRunnable instance

you will see

main thread: First thread 

printed. This is because the main thread builds the RecursiveRunnable, so

Thread.currentThread().setName(this.name); 

is actually changing the name of the main thread, not the thread that the Runnable will eventually run in.


Also

System.out.println(this.name); // displaying "First thread"

is referring to the name field of the RecursiveRunnable object, which you've set to the same value as the main thread.

Upvotes: 1

Related Questions