java.lang.ThreadLocal<?>
This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one has its own, independently initialized copy of the variable.
ThreadLocal instances are typically private static fields in classes that wish to associate the state with a thread.
Methods of ThreadLocal
1. ThreadLocal<Object>()
java.lang.ThreadLocal.ThreadLocal<Object>()
This method creates a thread-local variable.
Parameters: NA
Returns: NA
Exceptions: NA
Approach
Java
package com.ThreadLocal;public class ThreadLocal1 {public static void main(String[] args) {ThreadLocal<?> threadLocal = new ThreadLocal<>();System.out.println(threadLocal);}}
Output:
java.lang.ThreadLocal@1e6d1014
2. get()
Object java.lang.ThreadLocal.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.
Parameters: NA
Returns: the current thread's value of this thread-local.
Exceptions: NA
Approach
Java
package com.ThreadLocal;public class ThreadLocalget {public static void main(String[] args) {ThreadLocal<?> threadLocal = new ThreadLocal<>();System.out.println(threadLocal.get());}}
Output:
null
3. remove()
void java.lang.ThreadLocal.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.
Parameters: NA
Returns: NA
Exceptions: NA
Approach
Java
package com.ThreadLocal;public class ThreadLocalremove {public static void main(String[] args) {ThreadLocal<?> threadLocal = new ThreadLocal<>();threadLocal.remove();System.out.println("remove");}}
Output:
remove
4. withInitial(Supplier<? extends ?> supplier)
<?> ThreadLocal<?> java.lang.ThreadLocal.withInitial(Supplier<? extends ?> supplier)
This method takes one argument. This method creates a thread-local variable. The initial value of the variable is determined by invoking the get method on the Supplier.
Type Parameters: <S> the type of the thread local's value.
Parameters: One parameter is required for this method.
supplier: the supplier to be used to determine the initial value.
Returns: a new thread-local variable.
Throws:
1. NullPointerException - if the specified supplier is null.
Approach 1: When no exception
Java
package com.ThreadLocal;import java.util.function.Supplier;public class ThreadLocalwithInitial {public static void main(String[] args) {Supplier<?> supplier = new Supplier<>() {@Overridepublic String get() {return "hello";}};System.out.println(ThreadLocal.withInitial(supplier));}}
Output:
java.lang.ThreadLocal$SuppliedThreadLocal@4361bd48
Approach 2: NullPointerException
Java
package com.ThreadLocal;import java.util.function.Supplier;public class ThreadLocalwithInitial {public static void main(String[] args) {Supplier<?> supplier = null;System.out.println(ThreadLocal.withInitial(supplier));}}
Output:
Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.lang.ThreadLocal$SuppliedThreadLocal.<init>(ThreadLocal.java:300) at java.base/java.lang.ThreadLocal.withInitial(ThreadLocal.java:143) at com.ThreadLocal.ThreadLocalwithInitial.main(ThreadLocalwithInitial.java:9)
No comments:
Post a Comment