Core Java

ThreadLocal in Java – Example Program and Tutorial

ThreadLocal in Java is another way to achieve thread-safety apart from writing immutable classes. If you have been writing multi-threaded or concurrent code in Java then you must be familiar with the cost of synchronization or locking which can greatly affect the Scalability of application, but there is no choice other than synchronizing if you are sharing objects between multiple threads.

ThreadLocal in Java is a different way to achieve thread-safety, it doesn’t address synchronization requirement, instead, it eliminates sharing by providing an explicit copy of Object to each thread. Since Object is no more shared there is no requirement of Synchronization which can improve scalability and performance of the application.

In this Java ThreadLocal tutorial, we will see important points about ThreadLocal in Java, when to use ThreadLocal in Java, and a simple example of ThreadLocal in Java program.

When to use ThreadLocal in Java

Many Java Programmers question where to use ThreadLocal in Java and some even argue the benefit of the ThreadLocal variable, but ThreadLocal
has many genuine use cases and that’s why it’s added to the standard Java Platform Library. I agree though until you are not in concurrent programming, you will rarely use ThreadLocal. Below are some well know usage of ThreadLocal class in Java:

  1. ThreadLocal is fantastic to implement Per Thread Singleton classes or per thread context information like transaction id.
  2. You can wrap any non Thread Safe object in ThreadLocal and suddenly its uses become Thread-safe, as it’s only being used by Thread Safe. One of the classic examples of ThreadLocal is sharing SimpleDateFormat. Since SimpleDateFormat is not thread-safe, having a global formatter may not work but having per Thread formatter will certainly work.
  3. ThreadLocal provides another way to extend Thread. If you want to preserve or carry information from one method call to another you can carry it by using ThreadLocal
  4. This can provide immense flexibility as you don’t need to modify any method.

On a basic level, ThreadLocal provides Thread Confinement which is an extension of the local variable. while the local variable is only accessible on the block they are declared, ThreadLocal is visible only in Single Thread. 

No two Thread can see each other’s ThreadLocal variable. A real-Life example of ThreadLocal is in J2EE application servers which uses javaThreadLocal variable to keep track of transaction and security Context.

It makes a lot of sense to share heavy objects like Database Connection as
ThreadLocal in order to avoid excessive creation and cost of locking in case of sharing global instance.

Java ThreadLocal Example – Code

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author
 */
public class ThreadLocalTest {

    public static void main(String args[]) throws IOException {
        Thread t1 = new Thread(new Task());  
        Thread t2 = new Thread( new Task());
     
        t1.start();
        t2.start();      
     
    }
   
    /*
     * Thread safe format method because every thread will use its own DateFormat
     */
    public static String threadSafeFormat(Date date){
        DateFormat formatter = PerThreadFormatter.getDateFormatter();
        return formatter.format(date);
    }
   
}


/*
 * Thread Safe implementation of SimpleDateFormat
 * Each Thread will get its own instance of SimpleDateFormat which will not be shared between other threads. *
 */
class PerThreadFormatter {

    private static final ThreadLocal dateFormatHolder = new ThreadLocal() {

        /*
         * initialValue() is called
         */
        @Override
        protected SimpleDateFormat initialValue() {
            System.out.println("Creating SimpleDateFormat for Thread : " + Thread.currentThread().getName());
            return new SimpleDateFormat("dd/MM/yyyy");
        }
    };

    /*
     * Every time there is a call for DateFormat, ThreadLocal will return calling
     * Thread's copy of SimpleDateFormat
     */
    public static DateFormat getDateFormatter() {
        return dateFormatHolder.get();
    }
}

class Task implements Runnable{
   
    @Override
    public void run() {
        for(int i=0; i<2; i++){
            System.out.println("Thread: " + Thread.currentThread().getName() + " Formatted Date: " + ThreadLocalTest.threadSafeFormat(new Date()) );
        }      
    }
}

Output:
Creating SimpleDateFormat for Thread: Thread-0
Creating SimpleDateFormat for Thread: Thread-1
Thread: Thread-1 Formatted Date: 30/05/2012
Thread: Thread-1 Formatted Date: 30/05/2012
Thread: Thread-0 Formatted Date: 30/05/2012
Thread: Thread-0 Formatted Date: 30/05/2012

Output

Creating SimpleDateFormat for Thread: Thread-0
Creating SimpleDateFormat for Thread: Thread-1
Thread: Thread-1 Formatted Date: 30/05/2012
Thread: Thread-1 Formatted Date: 30/05/2012
Thread: Thread-0 Formatted Date: 30/05/2012
Thread: Thread-0 Formatted Date: 30/05/2012

If you look at the output of the above program then you will find that when a different thread calls getFormatter() method of ThreadLocal class than its call its initialValue() method which creates an exclusive instance of
SimpleDateFormat for that Thread. 

Since SimpleDateFormat is not shared between thread and essentially local to the thread which creates its own threadSafFormat() method is completely thread-safe.

Important points on Java ThreadLocal Class

  1. ThreadLocal in Java is introduced on JDK 1.2 but it later generified in JDK 1.4 to introduce type safety on ThreadLocal variable.

2. ThreadLocal can be associated with Thread scope, all the code which is executed by Thread has access to ThreadLocal variables but two thread can not see each other's ThreadLocal variable.

3. Each thread holds an exclusive copy of the ThreadLocal variable which becomes eligible to Garbage collection after thread finished or died, normally or due to any Exception, Given those ThreadLocal variable doesn't have any other live references.

4. ThreadLocal variables in Java are generally private static fields in Classes and maintain its state inside Thread.

We saw how ThreadLocal in Java opens another avenue for thread-safety. Though the concept of thread-safety by confining object to Thread is there from JDK 1.0 and many programmers have their own custom ThreadLocal
classes, having ThreadLocal in Java API makes it a lot more easy and standard. Think about the ThreadLocal variable while designing concurrency in your application. 

Don't misunderstand that ThreadLocal is an alternative to Synchronization, it all depends upon the design. If the design allows each thread to have their own copy of the object then ThreadLocal is there to use.

Other Java tutorials on Threading you may find useful

Thanks for reading this article so far. If you find this Infomation useful then please comment and share this article with your friends and colleagues. It makes a lot of difference. If you have any doubt, questions, or feedback, please ask and we would be happy to explain. 

Published on Java Code Geeks with permission by Javin Paul, partner at our JCG program. See the original article here: ThreadLocal in Java - Example Program and Tutorial

Opinions expressed by Java Code Geeks contributors are their own.

Javin Paul

I have been working in Java, FIX Tutorial and Tibco RV messaging technology from past 7 years. I am interested in writing and meeting people, reading and learning about new subjects.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Chris
Chris
3 years ago

I think line 38 needs the capitalization of “SimpleDateFormat” to be corrected. As shown currently as all lowercase, the class name won’t resolve.

Last edited 3 years ago by chris.nack
Back to top button