Java.lang.ThreadLocal Class in Java (original) (raw)
Last Updated : 13 Oct, 2022
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 Class
Method | Action Performed |
---|---|
get() | 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() method |
initialValue() | 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 thread |
set() | 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
class
GFG {
`` public
static
void
main(String[] args)
`` {
`` ThreadLocal<Number> gfg_local
`` =
new
ThreadLocal<Number>();
`` ThreadLocal<String> gfg =
new
ThreadLocal<String>();
`` gfg_local.set(
100
);
`` System.out.println(
"value = "
+ gfg_local.get());
`` gfg_local.set(
90
);
`` System.out.println(
"value = "
+ gfg_local.get());
`` gfg_local.set(
88.45
);
`` System.out.println(
"value = "
+ gfg_local.get());
`` gfg.set(
"GeeksforGeeks"
);
`` System.out.println(
"value = "
+ gfg.get());
`` }
}
Output
value = 100 value = 90 value = 88.45 value = GeeksforGeeks
Example 2:
Java
public
class
GFG {
`` public
static
void
main(String[] args)
`` {
`` ThreadLocal<Number> gfg_local
`` =
new
ThreadLocal<Number>();
`` ThreadLocal<String> gfg =
new
ThreadLocal<String>();
`` gfg_local.set(
100
);
`` System.out.println(
"value = "
+ gfg_local.get());
`` gfg_local.set(
90
);
`` System.out.println(
"value = "
+ gfg_local.get());
`` gfg_local.set(
88.45
);
`` System.out.println(
"value = "
+ gfg_local.get());
`` gfg.set(
"GeeksforGeeks"
);
`` System.out.println(
"value = "
+ gfg.get());
`` gfg.remove();
`` System.out.println(
"value = "
+ gfg.get());
`` gfg_local.remove();
`` System.out.println(
"value = "
+ gfg_local.get());
`` }
}
Output
value = 100 value = 90 value = 88.45 value = GeeksforGeeks value = null value = null
Example 3:
Java
import
java.lang.*;
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
(name);
`` start();
`` }
`` public
void
run()
`` {
`` for
(
int
i =
0
; i <
2
; i++)
`` System.out.println(getName() +
" "
+ gfg.get());
`` }
}
public
class
GFG {
`` public
static
void
main(String[] args)
`` {
`` NewThread t1 =
new
NewThread(
"quiz1"
);
`` NewThread t2 =
new
NewThread(
"quiz2"
);
`` }
}
Output: