Skip to main content

Managing Data with the ThreadLocal Class

ThreadLocal class allows you to put local data on a thread, so that every module running in the thread can access it. ThreadLocal has been around since JDK 1.2, but hasn't been used much, maybe because of a first, rather poor implementation, performance-wise.


Documentation From ThreadLocal Api

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. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID).

An example may help clarify this concept. A servlet is executed in a thread, but since many users may use the same servlet at the same time, many threads will be running the same servlet code concurrently. If the servlet uses a ThreadLocal object, it can hold data local to each thread. The user ID is a good example of what could be stored in the ThreadLocal object. I like to think of this object as a hash map where a kind of thread ID is used as the key.
ThreadLocal contains these methods:
Method
Purpose
Object get()
Returns the value for the current thread
set(Object)
Sets a new value for the current thread
Object initialValue()
Used to return an initial value (if ThreadLocal is subclassed)
remove()
In JDK 5 only - used to delete the current thread's value (for clean-up only)
The simplest way to use a ThreadLocal object is to implement it as a singleton. Here's an example in which the value stored in the ThreadLocal is a List:







public class MyThreadLocal {

private static ThreadLocal tLocal = new ThreadLocal();

public static void set(List list) {
tLocal.set(list);
}

public static List get() {
return (List) tLocal.get();
}




makes it simple to set or get the current thread's value:

MyThreadLocal.set(list);
. . .
list = MyThreadLocal.get();



The first time you use this technique, it may seem a bit like magic, but behind the scenes, the local data is simply fetched using a unique ID of the thread.

Comments

Popular posts from this blog

JSF Lifecycle Phases

JSF Lifecycle Phases JSF follows MVC design pattern to handle request-response process. Basically JSF handles three types of requests. Non-Faces Request Generates Faces Response Faces Request Generates Non-Faces Response Faces Request Generates Faces Response In another case of course non-jsf to non-jsf is there but in this case there is no involvement of JSF action here, so this is not a part of jsf lifecycle process. A JavaServer Faces page is represented by a tree of UI components, called a view . When a client makes a request for the page, the life cycle starts. During the life cycle, the JavaServer Faces implementation must build the view while considering state saved from a previous submission of the page. When the client submits a page, the JavaServer Faces implementation must perform several tasks, such as validating the data input of components in the view and converting input data to types specified on the server side. The JavaServer Faces implementation performs a...

Flex Component Life Cycle

Flex Component Life Cycle Flex component life cycle is useful when you want to create your own component to suit your application requirement. The entire process has been divided into three broad categories. 1. Initialization phase 2. Update Phase 3. Destruction Phase Initialization Phase: The first phase of component life cycle. Again it contains four stages. 1. construction Stage 2. Configuration Stage 3. Attachment Stage 4. Initialization Stage Construction Stage: The constructor of the component is called by the MXML component tag or by using the new operator of Action script class. This is the very first stage where the component life cycle begins. Constructor of the component calls the super () to invoke the super class constructor. Commonly we create out custom component by extending the UIComponent class, as it is base class for all display components of flex framework. The stage is used to: i) set some initial values for component properties ii) add event listener to the...

Hibernate

Best sites which are very much useful for assist in Hibernate related problems * Hibernate main site * Hibernate Tutorial * Hibernate Forum * Hibernate Wikipedia * Popular Hibernate Books * Hibernate Materials * Relational Persistence for Idiomatic Java