Java Reference
In-Depth Information
The initialValue() method sets the initial value of the thread-local variable for each thread. If you have set
the initial value, the call to the get() method, before you call the set() method, will return that initial value. It is a
protected method. You must override it in a subclass. You can set the initial value for the call counter to 1000 by using
an anonymous class as shown:
// Create an anonymous subclass ThreadLocal class and override its initialValue() method to
// return 1000 as the initial value
private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>() {
@Override
public Integer initialValue() {
return 1000;
}
};
Subclassing the ThreadLocal class just to have an instance of ThreadLocal with an initial value was overkill.
Finally, the class designers realized it (in Java 8) and provided a factory method called withInitial() in the
ThreadLocal class that can specify an initial value. The method is declared as follows:
public static <S> ThreadLocal<S> withInitial(Supplier<? extends S> supplier)
The specified supplier provides the initial value for the ThreadLocal . The get() method of the supplier is used
to get the initial value. You can rewrite above logic and replace the anonymous class, like so:
// Create a ThreadLocal with an initial value of 1000
ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 1000);
Having a Supplier as the supplier for the initial value, you can generate the initial value lazily and based on
some logic. The following statement creates a ThreadLocal with initial value as the second part of the current time
when the initial value is retrieved:
// Return the second of the current time as the initial value
ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> LocalTime.now().getSecond());
You can use the remove() method to reset the value of the thread-local variable for a thread. After the call to the
remove() method, the first call to the get() method works as if it was called the first time by returning the initial value.
The typical use of a thread-local variable is to store user id, transaction id, or transaction context for a thread.
The thread sets those values in the beginning, and any code during the execution of that thread can use those values.
Sometimes a thread may start child threads that may need to use the value set for a thread-local variable in the parent
thread. You can achieve this by using an object of the InheritableThreadLocal class, which is inherited from the
ThreadLocal class. The child thread inherits its initial value from the parent thread. However, the child thread can set
its own value using the set() method.
Setting Stack Size of a Thread
Each thread in a JVM is allocated its own stack. A thread uses its stack to store all local variables during its
execution. Local variables are used in constructors, methods, or blocks ( static or non-static). The stack size of
each thread will limit the number of threads that you can have in a program. Local variables are allocated memory
on stack during their scope. Once they are out of scope, the memory used by them is reclaimed. It is essential
to optimize the stack size of a thread in your program if it uses too many threads. If the stack size is too big, you
can have a fewer number of threads in your program. The number of threads will be limited by the available
 
Search WWH ::




Custom Search