Java ThreadLocal set() Method



Description

The Java ThreadLocal set() method sets the current thread's copy of this thread-local variable to the specified value.

Declaration

Following is the declaration for java.lang.ThreadLocal.set() method

public void set(T value)

Parameters

value − This is the value to be stored in the current thread's copy of this thread-local.

Return Value

This method does not return any value.

Exception

NA

Example: Setting an Integer Value in ThreadLocal Object

The following example shows the usage of Java ThreadLocal set() method. In this program, we've initialized a ThreadLocal object. Using set() method, a value is assigned to ThreadLocal object and using get() method, value is retrieved and printed. Same process is repeated with new value and result is printed.

package com.tutorialspoint;

public class ThreadLocalDemo {

   public static void main(String[] args) {

      ThreadLocal<Integer> tlocal = new ThreadLocal<>();  

      tlocal.set(100);
      // returns the current thread's value
      System.out.println("value = " + tlocal.get());
 
      tlocal.set(90);
      // returns the current thread's value of 
      System.out.println("value = " + tlocal.get());
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

value = 100
value = 90

Example: Setting a Double Value in ThreadLocal Object

The following example shows the usage of Java ThreadLocal set() method. In this program, we've initialized a ThreadLocal object. Using set() method, a value is assigned to ThreadLocal object and using get() method, value is retrieved and printed. Same process is repeated with new value and result is printed.

package com.tutorialspoint;

public class ThreadLocalDemo {

   public static void main(String[] args) {

      ThreadLocal<Double> tlocal = new ThreadLocal<>();  

      tlocal.set(100.0);
      // returns the current thread's value
      System.out.println("value = " + tlocal.get());
 
      tlocal.set(90.0);
      // returns the current thread's value of 
      System.out.println("value = " + tlocal.get());
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

value = 100.0
value = 90.0

Example: Setting a String Value in ThreadLocal Object

The following example shows the usage of Java ThreadLocal set() method. In this program, we've initialized a ThreadLocal object. Using set() method, a value is assigned to ThreadLocal object and using get() method, value is retrieved and printed. Same process is repeated with new value and result is printed.

package com.tutorialspoint;

public class ThreadLocalDemo {

   public static void main(String[] args) {

      ThreadLocal<String> tlocal = new ThreadLocal<>();  

      tlocal.set("100");
      // returns the current thread's value
      System.out.println("value = " + tlocal.get());
 
      tlocal.set("90");
      // returns the current thread's value of 
      System.out.println("value = " + tlocal.get());
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

value = 100
value = 90
java_lang_threadlocal.htm
Advertisements