Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8184865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:39:20+00:00 2026-06-07T01:39:20+00:00

The javadoc about the class java.lang.ThreadLocal is making me confused. They are saying that

  • 0

The javadoc about the class java.lang.ThreadLocal is making me confused. They are saying that each thread that access a thread-local variable has its own, independently initialized copy of the variable.
Here is an example (not a true life example) who proves that variable held in a thread-local variable could be shared by many threads:

package com.mohamad.test.threadlocal;

import java.util.List;

public class ThreadLocalExample {

    private static final ThreadLocal<List<Integer>> myThreadLocal = new ThreadLocal<List<Integer>>();

    public static List<Integer> get() {
        return (myThreadLocal.get());
    }

    public static void set(List<Integer> value) {
        myThreadLocal.set(value);
    }
}


package com.mohamad.test.threadlocal;

import java.util.ArrayList;
import java.util.List;


public class TestThreadLocal implements Runnable {

    private static List<Integer> MY_TEST_LIST = new ArrayList<Integer>(){
        /** The serialVersionUID */
        private static final long serialVersionUID = -2419885728976816054L;
        {add(1);}
    };


    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run() {
        ThreadLocalExample.set(MY_TEST_LIST);
        List<Integer> integers = ThreadLocalExample.get();
        integers.remove(0);
        System.out.println(Thread.currentThread().getName() + " finished successfully, The list's size is: "  + ThreadLocalExample.get().size() + "\n");
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        TestThreadLocal thread1 = new TestThreadLocal();
        Thread t1 = new Thread(thread1);
        t1.start();
        TestThreadLocal thread2 = new TestThreadLocal();
        Thread t2 = new Thread(thread2);
        t2.start();
    }
}

If we run this exemple, a java.lang.IndexOutOfBoundsException will be thrown because of the MY_TEST_LIST that is shared by thread1 and thread2. (And as we saw, when thread1 and thread2 called the set(MY_TEST_LIST) method of ThreadLocalExample, that called the set method of the ThreadLocal variable, it didn’t create an independent local copy of the MY_TEST_LIST)

If somebody has already asked this question, please give the link to the answer because i did’t find anything interesting while making a research on google.

Regards,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:39:22+00:00Added an answer on June 7, 2026 at 1:39 am

    Everything is fine. Variable hold in ThreadLocal is local to the thread. In your case it is a reference that is local, not the list itself. Each thread has its own copy of the reference, but all these references point to the same location. In other words: each thread can keep a reference to a different List but in your case they all point to the same one.

    If you want your example to work, each ThreadLocal should point to a different ArrayList (a copy):

    myThreadLocal.set(new ArrayList<Integer>(value));
    

    Having ThreadLocals all pointing to the same object doesn’t make much sense as in this case you only need a single, globally available reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It is said that in the Spring javadoc article about DriverManagerDataSource class, that this
Accoriding to javadoc, public class AtomicInteger extends Number implements java.io.Serializable { // code for
Can anyone give me short description about his spring class org.springframework.jdbc.core.BatchPreparedStatementSetter ( JavaDoc API
I am a little bit confused about the ComponentName class in Android. There are
The Javadoc about String.intern() doesn't give much detail. (In a nutshell: It returns a
I've been reading about ThreadLocal, trying to understand how it works and why we
Below is an example that is producing an exception in Java (and not matching
Clearly these are a class, right? But I can't find any javadoc api's on
Android's AlarmManager Javadoc states When an alarm goes off, the Intent that had been
Is it possible, when you mouse hover on method/class so you can see Javadoc

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.