Amit
Amit

Reputation: 388

ThreadLocal vs thread local variable

What is difference in ThreadLocal.get/put and

class ThreadT extends Thread {
private SomeObj obj;
.....
}

I believe, please correct me if I am wrong, this obj will also be different for each thread. i.e., if we are having 5 objects of the ThreadT, we will be having each five of them having different objects of obj.

So if this is the case, then why do we need to have the need to use ThreadLocal?

Please correct me if I my understanding about either of the two is incorrect.

Upvotes: 5

Views: 4164

Answers (3)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

The fact that your class extends Thread does not make its fields special. Assuming your class has get / set methods

    final ThreadT tt = new ThreadT();

    new Thread() {
        public void run() {
            tt.set(new Object());
        };
    }.start();

    new Thread() {
        public void run() {
            Object obj = tt.get();
        };
    }.start();

the second thread will get the object which first thread put. It wouldnt happen with ThreadLocal

Upvotes: 4

abishkar bhattarai
abishkar bhattarai

Reputation: 7641

I have written program

class ThreadRun implements Runnable {

    NumberValue number;
    int value;

    ThreadRun(NumberValue number,int value) {


        this.number=number;
        this.value = value;
        Thread t = new Thread(this);
        t.start();
    }

    @Override
    public void run() {

        number = number.getThreadLocal();


        number.setId(number.getId()+value);

        System.out.println(number.getId());

    }

}

public class ThreadTest {
    public static void main(String[] args) {
        NumberValue number = new NumberValue(1);


        new ThreadRun(number, 1);
        new ThreadRun(number, 2);
    }
}

class NumberValue {

    int id;



    ThreadLocal<NumberValue> threadLocal = new ThreadLocal<NumberValue>() {

    @Override
    protected NumberValue initialValue() {

        return new NumberValue(id);
    }
    };



    NumberValue(int id) {
        this.id = id;

    }



    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
    public NumberValue getThreadLocal() {
        return threadLocal.get();
    }






}

output: 2 3 this output is our expected output

But when you comment

// number = number.getThreadLocal(); and run program output will be output: 3 4 So the data is corrupted when // number = number.getThreadLocal(); is commented.So threadlocal make a local copy of NumberValue .But when threadlocal is not used same object instance is shared between threads ,so data result is corrupted than actaul result

Upvotes: 0

Thihara
Thihara

Reputation: 6969

From the documentation

This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable.

Your approach will work if you are writing classes that are directly going to extend thread. But what about classes that need a ThreadLocal variable but doesn't have direct access to it's Thread instance?

In such cases ThreadLocal is useful. Specially in server environments where you don't directly use the threads most of the time.

Upvotes: 6

Related Questions