Java — ThreadLocal | Code Factory

Index Page : Link

Donate : Link

WordPress Blog : Link

  • ThreadLocal class provides thread local variable.

Note :

  1. ThreadLocal class introduced in 1.2v and enhanced in 1.5v.

Constructor :

1. ThreadLocal tl = new ThreadLocal();

  • Creates a thread local variable.

Methods :

1. public T get()

  • Returns the value of ThreadLocal variable associated with current thread.

2. protected Object initialValue()

  • Returns initial value of ThreadLocal variable associated with current thread.

3. public void set(Object value)

  • To set a new value

4. public void remove()

  • To remove the value of ThreadLocal variable associated with current thread.

Example 1 :

package com.example.thread;/**
* @author code.factory
*
*/
public class ThreadLocalTest {
public static void main(String... args) {
ThreadLocal tl = new ThreadLocal();
System.out.println(tl.get()); // null
tl.set("Code");
System.out.println(tl.get()); // Code
tl.remove();
System.out.println(tl.get()); // null
}
}

Example 2 :

package com.example.thread;/**
* @author code.factory
*
*/
public class ThreadLocalTest {
public static void main(String... args) {
ThreadLocal tl = new ThreadLocal() {
public Object initialValue() {
return "Hello";
}
};
System.out.println(tl.get()); // Hello
tl.set("Code");
System.out.println(tl.get()); // Code
tl.remove();
System.out.println(tl.get()); // Hello
}
}

Example 3 :

package com.example.thread;/**
* @author code.factory
*
*/
public class ThreadLocalTest {
public static void main(String... args) {
CustomerThread c1 = new CustomerThread("Customer 1");
CustomerThread c2 = new CustomerThread("Customer 2");
CustomerThread c3 = new CustomerThread("Customer 3");
CustomerThread c4 = new CustomerThread("Customer 4");
c1.start();
c2.start();
c3.start();
c4.start();
}
}
class CustomerThread extends Thread {
static Integer custId = 0;

private static ThreadLocal tl = new ThreadLocal() {
protected Integer initialValue() {
// synchronized(this) { // #1
return ++custId;
// } // #2
}
};

CustomerThread(String name) {
super(name);
}

public void run() {
System.out.println(Thread.currentThread().getName() + " customer id : " + tl.get());
}
}

Output :

Customer 4 customer id : 3
Customer 2 customer id : 2
Customer 3 customer id : 4
Customer 1 customer id : 1
  • In the above program for every customer thread a seperate customer id will be maintained by ThreadLocal object.
  • Parent thread’s ThreadLocal variable by default not available to the child thread.

Constructors :

1. InheritableThreadLocal t = new InheritableThreadLocal();

Methods :

  • InheritableThreadLocal is a child class of ThreadLocal and hence all method’s present in ThreadLocal by default available to InheritableThreadLocal.
package com.example.thread;/**
* @author code.factory
*
*/
public class ThreadLocalTest {
public static void main(String... args) {
ParentThread pt = new ParentThread();
pt.start();
}
}
class ParentThread extends Thread {
public static InheritableThreadLocal l = new InheritableThreadLocal() {
public Object childValue(Object p) {
return "Child";
}
};

public void run() {
l.set("Parent");
System.out.println("Parent thread value : " + l.get());
ChildThread ct = new ChildThread();
ct.start();
}
}
class ChildThread extends Thread {
public void run() {
System.out.println("Child thread value : " + ParentThread.l.get());
}
}

Output :

Parent thread value : Parent
Child thread value : Child
  • In the above program if replace InheritableThreadLocal with the ThreadLocal and if we are not overriding childValue() method then the output is
Parent thread value : Parent
Child thread value : null
  • In the above program if we are maintaining InheritableThreadLocal and if we are not overriding childValue() then the output is
Parent thread value : Parent
Child thread value : Parent

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store