Make Selenium Easy

Importance of Using ThreadLocal Variables in Parallel Execution Of Automation Scripts

In previous post, we see some common mistakes which we do while running selenium automation scripts in parallel. As we know now that object variables are shared by threads created by same object, we need a way to keep object variables unique to each threads so that we don’t get any data inconsistency when reading or writing same variables.

To achieve the same we can use ThreadLocal class of Java. Let me give you a basic idea about it and you will understand how class name itself gives you a hint about it.

If you want that a variable should be accessed by same thread by which it is created, we can use ThreadLocal variables. This class makes variables thread safe. Each thread of same object will have separate copy of object variables.

Official Javadoc defines ThreadLocal class as :-

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). Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are subject to garbage collection (unless other references to these copies exist).

ThreadLocal class provides below methods:-

  1. initialValue() :- Returns the current thread’s “initial value” for this thread-local variable
  2. set(T value) :- Sets the current thread’s copy of this thread-local variable to the specified value
  3. get() :- 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.
  4. remove() :- Removes the current thread’s value for this thread-local variable.

Just to summarize, A ThreadLocal variable could be initialized with an initial value by overriding initialValue() method. Use set() method to set the value for variable , get() to get it and remove() to remove it. There are plenty of examples given on ThreadLocal on internet. Refer JournalDev and Jenkov. These two posts are great.

Let’s rewrite the same RegisterUser class from previous post with a ThreadLocal variable.

Since class variable “resgiteredUserName” will be shared among threads so lets have it as a ThreadLocal variable.

Test Class :-

Run above class and observe output.

Observe that value of registeredUserName is not reused by another thread as both threads have their own copy of object variable named “registeredUserName”.

Let’s rewrite Selenium example from previous post using ThreadLocal class.

When we run @Test annotated methods in parallel, each test methods will be run by a different thread. Both threads will be sharing WebDriver class variable. So let’s declare it as ThreadLocal.

WebDriverFactory class:-

Test Class:-

Run above tests parallel using testng xml as below:-


Both threads have control on browser instance initiated by them now and there will be no session overriding by threads.

Note:- Since I am running test methods in parallel so I need to apply Thread Safe at TestNG class level. If you are running classes or tests in parallel, you need to identify shared resource and make them thread safe. We will see this example in next post.

You can download code form this repo.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

6 thoughts on “Importance of Using ThreadLocal Variables in Parallel Execution Of Automation Scripts

  1. Hello Amod,
    Thanks for such detailed content . i was able to achieve parallel execution due to our post . Great work .All the Best 🙂 Hearty Thanks

  2. Hello Amod, Thanks for this article.

    I am trying to implement parallel execution in my Junit 5 Automation framework (@Execution(ExecutionMode.concurrent)) but though it opens 2 browsers for my test cases but it executes only 1 test case in browser 1 and once it is complete then only it goes to the other browser for testcase 2. What can be the issue here ?
    Test Cases flow :
    1. BrowserDriver .java class has the ThreadLocal webdriver variable and browser launch methods and also the getDriver method.
    2. Test cases are in a separate class which extends the class.
    3. @BeforeEach / @AfterEach, @BeforeAll/@AfterAll – are all present in BaseTestCase .java
    4. Browser initiation , Data Load all happens in @BeforeEach Method.

    Can you pls help me why am i seeing this issue ?

    1. Hi,
      Only using ThreadLocal doesn’t solve all issues in parallel execution. If you are running all test methods in parallel, make sure all initialisation happen for each method call.

  3. Hello Amod,

    Thanks for a wonderful article. After referring this article, I tried to implement the same in my sample project. I was unable to run it successfully. The actions are getting clubbed on single browser and thus unstable. Is this approach feasible in Page Object Model ?

    Deven J.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.