This class provides thread local variable. 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 an another way to achieve thread safety apart from writing immutable classes.
- Since Object is no more shared there is no requirement of Synchronization which can improve scalability and performance of application.
- It extends class Object.
- ThreadLocal provides thread restriction which is extension of local variable. ThreadLocal are visible only in single thread. No two thread can see each others thread local variable.
- These variable are generally private static field in classes and maintain its state inside thread.
Constructor:
ThreadLocal(): This creates a thread local variable.
Methods:
-
Object get(): This method 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.
Syntax: public Object get(). Returns: the current thread's value of this thread-local. Exception: NA.
-
void set(Object value): This method 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.
Syntax: public void set(T value). Returns: NA. Exception: NA.
// Java code illustrating get() and set() method
public
class
ThreadLocalDemo {
public
static
void
main(String[] args)
{
ThreadLocal<Number> gfg_local =
new
ThreadLocal<Number>();
ThreadLocal<String> gfg =
new
ThreadLocal<String>();
// setting the 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"
);
// returns the current thread's value of
System.out.println(
"value = "
+ gfg.get());
}
}
Output:
value = 100 value = 90 value = 88.45 value = GeeksforGeeks
-
void remove(): This method 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.
Syntax: public void remove(). Returns: NA. Exception: NA.
// Java code illustrating remove() method
public
class
ThreadLocalDemo {
public
static
void
main(String[] args)
{
ThreadLocal<Number> gfg_local =
new
ThreadLocal<Number>();
ThreadLocal<String> gfg =
new
ThreadLocal<String>();
// setting the 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"
);
// returns the current thread's value of
System.out.println(
"value = "
+ gfg.get());
// removing value
gfg.remove();
// returns the current thread's value of
System.out.println(
"value = "
+ gfg.get());
// removing vale
gfg_local.remove();
// returns the current thread's value of
System.out.println(
"value = "
+ gfg_local.get());
}
}
Output:
value = 100 value = 90 value = 88.45 value = GeeksforGeeks value = null value = null
-
Object initialValue(): This method returns the current thread’s initial value for this thread-local variable.
Syntax: protected Object initialValue() Returns: the initial value for this thread-local. Exception: NA.
// Java code illustrating initialValue() method
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
ThreadLocalDemo {
public
static
void
main(String[] args)
{
NewThread t1 =
new
NewThread(
"quiz1"
);
NewThread t2 =
new
NewThread(
"quiz2"
);
}
}
Output:
quiz2 14 quiz1 15 quiz1 15 quiz2 14
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
leave a comment
0 Comments