Java.lang.ThreadLocal Class in Java Last Updated : 13 Oct, 2022 Comments Improve Suggest changes Like Article Like Report 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. Basically, it is another way to achieve thread safety apart from writing immutable classes. Since the Object is no more shared there is no requirement for Synchronization which can improve the scalability and performance of the application. ThreadLocal provides thread restriction which is an extension of a local variable. ThreadLocal is visible only in a single thread. No two threads can see each other's thread-local variable. These variables are generally private static fields in classes and maintain their state inside the thread. Note: ThreadLocal class extends Object class Constructor: ThreadLocal(): This creates a thread-local variable. Methods of ThreadLocal ClassMethodAction Performedget()Returns the value in the current thread's copy of this thread-local variable. If the variable has no value for the current thread, it is first initialized to the value returned by an invocation of the initialValue() methodinitialValue()Returns the current thread initial value for the local thread variable. remove()Removes the current thread's value for this thread-local variable. If this thread-local variable is subsequently read by the current thread, its value will be reinitialized by invoking its initialValue() method, unless its value is set by the current thread in the interim. This may result in multiple invocations of the initialValue method in the current threadset()Sets the current thread's copy of this thread-local variable to the specified value. Most subclasses will have no need to override this method, relying solely on the initialValue() method to set the values of thread locals. Example 1: Java // Java Program to Illustrate ThreadLocal Class // Via get() and set() Method // Class // ThreadLocalDemo class GFG { // Main driver method public static void main(String[] args) { // Creating objects of ThreadLocal class ThreadLocal<Number> gfg_local = new ThreadLocal<Number>(); ThreadLocal<String> gfg = new ThreadLocal<String>(); // Now setting custom value gfg_local.set(100); // Returns the current thread's value System.out.println("value = " + gfg_local.get()); // Setting the value gfg_local.set(90); // Returns the current thread's value of System.out.println("value = " + gfg_local.get()); // Setting the value gfg_local.set(88.45); // Returns the current thread's value of System.out.println("value = " + gfg_local.get()); // Setting the value gfg.set("GeeksforGeeks"); // Returning the current thread's value of System.out.println("value = " + gfg.get()); } } Outputvalue = 100 value = 90 value = 88.45 value = GeeksforGeeks Example 2: Java // Java Program to Illustrate ThreadLocal Class // Via Illustrating remove() Method // Class // ThreadLocalDemo public class GFG { // Main driver method public static void main(String[] args) { // Creating objects of ThreadLocal class ThreadLocal<Number> gfg_local = new ThreadLocal<Number>(); ThreadLocal<String> gfg = new ThreadLocal<String>(); // Setting the value gfg_local.set(100); // Returning the current thread's value System.out.println("value = " + gfg_local.get()); // Setting the value gfg_local.set(90); // Returns the current thread's value of System.out.println("value = " + gfg_local.get()); // Setting the value gfg_local.set(88.45); // Returning the current thread's value of System.out.println("value = " + gfg_local.get()); // Setting the value gfg.set("GeeksforGeeks"); // Returning the current thread's value of System.out.println("value = " + gfg.get()); // Removing value using remove() method gfg.remove(); // Returning the current thread's value of System.out.println("value = " + gfg.get()); // Removing value gfg_local.remove(); // Returns the current thread's value of System.out.println("value = " + gfg_local.get()); } } Outputvalue = 100 value = 90 value = 88.45 value = GeeksforGeeks value = null value = null Example 3: Java // Java Program to Illustrate ThreadLocal Class // Via initialValue() Method // Importing required classes import java.lang.*; // Class 1 // Helper class extending Thread class class NewThread extends Thread { private static ThreadLocal gfg = new ThreadLocal() { protected Object initialValue() { return new Integer(question--); } }; private static int question = 15; NewThread(String name) { // super keyword refers to parent class instance super(name); start(); } // Method // run() method for Thread public void run() { for (int i = 0; i < 2; i++) System.out.println(getName() + " " + gfg.get()); } } // Class 2 // Main class // ThreadLocalDemo public class GFG { // Main driver method public static void main(String[] args) { // Creating threads inside main() method NewThread t1 = new NewThread("quiz1"); NewThread t2 = new NewThread("quiz2"); } } Output: Comment More infoAdvertise with us Next Article Java.lang.ThreadLocal Class in Java A Abhishek Verma Improve Article Tags : Java Practice Tags : Java Similar Reads Java.lang.Void Class in Java Java.lang.Void class is a placeholder that holds a reference to a class object if it represents a void keyword. It is an uninstantiable placeholder. Well, uninstantiable means that this class has a private constructor and no other constructor that we can access from outside. Methods of lang.void cla 1 min read java.lang.management.ThreadInfo Class in Java java.lang.management.ThreadInfo class contains methods to get information about a thread. This information includes: Thread IDThread NameState of the ThreadStack trace of the ThreadThe object upon which the Thread is blockedList of object monitors that are blocked by the ThreadList of ownable synchr 4 min read java.time.LocalDate Class in Java Java is the most popular programming language and widely used programming language. Java is used in all kind of application like as mobile application, desktop application, web application. In this Java java.time.LocalDate class is imported which represents to display the current date. java.time: It 4 min read Java.lang package in Java Java.lang package in JavaProvides classes that are fundamental to the design of the Java programming language. The most important classes are Object, which is the root of the class hierarchy, and Class, instances of which represent classes at run time. Following are the Important Classes in Java.lan 3 min read Java.util.Locale Class in Java | Set 1 As the name suggests util.Locale Class is used to perform locale tasks and provides locale information for the user. Declaration : public final class Locale extends Object implements Cloneable, Serializable Constructors : Locale(String L): Creates Locale from the given language code.Locale(String L, 4 min read ThreadLocalRandom vs SecureRandom Class in Java ThreadLocalRandom class of java.util package is a random number generator that generates random numbers isolated to the current thread. It is a subclass of the Random class. It is initialized with an internally generated seed value that cannot be modified. The class view is as follows: --> java.u 4 min read Java.util.Locale Class in Java | Set 2 Java.util.Locale Class in Java | Set 1 More methods: getDisplayVariant() : java.util.Locale.getDisplayVariant() displays variant of the Locale Syntax : public final String getDisplayVariant() Parameters : ---- Return : ----------- getDisplayVariant(Locale in) : java.util.Locale.Locale in(Locale in) 3 min read Class Level Lock in Java Every class in Java has a unique lock which is nothing but class level lock. Â If a thread wants to execute a static synchronized method, then the thread requires a class level lock. Class level lock prevents multiple threads to enter a synchronized block in any of all available instances of the clas 5 min read java.time.LocalDateTime Class in Java java.time.LocalDateTime class, introduced in Java 8, represents a local date-time object without timezone information. The LocalDateTime class in Java is an immutable date-time object that represents a date in the yyyy-MM-dd-HH-mm-ss.zzz format. It implements the ChronoLocalDateTime interface and in 4 min read Local Inner Class in Java Prerequisites: Nested Classes in Java Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. Sometimes this block can be a for loop or an if clause. Local Inner classes are not a member of any enclosing classes. They belong to the block the 5 min read Like