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

  • Home
  • SEARCH
  • 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 7410053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:10:25+00:00 2026-05-29T06:10:25+00:00

After looking at this question , I think I want to wrap ThreadLocal to

  • 0

After looking at this question, I think I want to wrap ThreadLocal to add a reset behavior.

I want to have something similar to a ThreadLocal, with a method I can call from any thread to set all the values back to the same value. So far I have this:

public class ThreadLocalFlag {

    private ThreadLocal<Boolean> flag;
    private List<Boolean> allValues = new ArrayList<Boolean>();

    public ThreadLocalFlag() {
        flag = new ThreadLocal<Boolean>() {
            @Override protected Boolean initialValue() {
                Boolean value = false;
                allValues.add(value);
                return value;
            }
        };
    }

    public boolean get() {
        return flag.get();
    }

    public void set(Boolean value) {
        flag.set(value);
    }

    public void setAll(Boolean value) {
        for (Boolean tlValue : allValues) {
            tlValue = value;
        }
    }
}

I’m worried that the autoboxing of the primitive may mean the copies I’ve stored in the list will not reference the same variables referenced by the ThreadLocal if I try to set them. I’ve not yet tested this code, and with something tricky like this I’m looking for some expert advice before I continue down this path.

Someone will ask “Why are you doing this?”. I’m working in a framework where there are other threads that callback into my code, and I don’t have references to them. Periodically I want to update the value in a ThreadLocal variable they use, so performing that update requires that the thread which uses the variable do the updating. I just need a way to notify all these threads that their ThreadLocal variable is stale.


I’m flattered that there is new criticism recently regarding this three year old question, though I feel the tone of it is a little less than professional. The solution I provided has worked without incident in production during that time. However, there are bound to be better ways to achieve the goal that prompted this question, and I invite the critics to supply an answer that is clearly better. To that end, I will try to be more clear about the problem I was trying to solve.

As I mentioned earlier, I was using a framework where multiple threads are using my code, outside my control. That framework was QuickFIX/J, and I was implementing the Application interface. That interface defines hooks for handling FIX messages, and in my usage the framework was configured to be multithreaded, so that each FIX connection to the application could be handled simultaneously.

However, the QuickFIX/J framework only uses a single instance of my implementation of that interface for all the threads. I’m not in control of how the threads get started, and each is servicing a different connection with different configuration details and other state. It was natural to let some of that state, which is frequently accessed but seldom updated, live in various ThreadLocals that load their initial value once the framework has started the thread.

Elsewhere in the organization, we had library code to allow us to register for callbacks for notification of configuration details that change at runtime. I wanted to register for that callback, and when I received it, I wanted to let all the threads know that it’s time to reload the values of those ThreadLocals, as they may have changed. That callback comes from a thread I don’t control, just like the QuickFIX/J threads.

My solution below uses ThreadLocalFlag (a wrapped ThreadLocal<AtomicBoolean>) solely to signal the other threads that it may be time to update their values. The callback calls setAll(true), and the QuickFIX/J threads call set(false) when they begin their update. I have downplayed the concurrency issues of the ArrayList because the only time the list is added to is during startup, and my use case was smaller than the default size of the list.

I imagine the same task could be done with other interthread communication techniques, but for what it’s doing, this seemed more practical. I welcome other solutions.

  • 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-05-29T06:10:27+00:00Added an answer on May 29, 2026 at 6:10 am

    I’m disappointed in the quality of the answers received for this question; I have found my own solution.

    I wrote my test case today, and found the only issue with the code in my question is the Boolean. Boolean is not mutable, so my list of references wasn’t doing me any good. I had a look at this question, and changed my code to use AtomicBoolean, and now everything works as expected.

    public class ThreadLocalFlag {
    
        private ThreadLocal<AtomicBoolean> flag;
        private List<AtomicBoolean> allValues = new ArrayList<AtomicBoolean>();
    
        public ThreadLocalFlag() {
            flag = new ThreadLocal<AtomicBoolean>() {
                @Override protected AtomicBoolean initialValue() {
                    AtomicBoolean value = new AtomicBoolean();
                    allValues.add(value);
                    return value;
                }
            };
        }
    
        public boolean get() {
            return flag.get().get();
        }
    
        public void set(boolean value) {
            flag.get().set(value);
        }
    
        public void setAll(boolean value) {
            for (AtomicBoolean tlValue : allValues) {
                tlValue.set(value);
            }
        }
    }
    

    Test case:

    public class ThreadLocalFlagTest {
    
        private static ThreadLocalFlag flag = new ThreadLocalFlag();
        private static boolean runThread = true;
    
        @AfterClass
        public static void tearDownOnce() throws Exception {
            runThread = false;
            flag = null;
        }
    
        /**
         * @throws Exception if there is any issue with the test
         */
        @Test
        public void testSetAll() throws Exception {
            startThread("ThreadLocalFlagTest-1", false);
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                //ignore
            }
            startThread("ThreadLocalFlagTest-2", true);
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                //ignore
            }
            startThread("ThreadLocalFlagTest-3", false);
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                //ignore
            }
            startThread("ThreadLocalFlagTest-4", true);
            try {
                Thread.sleep(8000L); //watch the alternating values
            } catch (InterruptedException e) {
                //ignore
            }
            flag.setAll(true);
            try {
                Thread.sleep(8000L); //watch the true values
            } catch (InterruptedException e) {
                //ignore
            }
            flag.setAll(false);
            try {
                Thread.sleep(8000L); //watch the false values
            } catch (InterruptedException e) {
                //ignore
            }
        }
    
        private void startThread(String name, boolean value) {
            Thread t = new Thread(new RunnableCode(value));
            t.setName(name);
            t.start();
        }
    
        class RunnableCode implements Runnable {
    
            private boolean initialValue;
    
            RunnableCode(boolean value) {
                initialValue = value;
            }
    
            @Override
            public void run() {
                flag.set(initialValue);
                while (runThread) {
                    System.out.println(Thread.currentThread().getName() + ": " + flag.get());
                    try {
                        Thread.sleep(4000L);
                    } catch (InterruptedException e) {
                        //ignore
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking around on SO and I don't -think- this is something that's
Looking at the related questions, I don't think this specific question has been asked,
I ask this not to start anything negative. Rather, after looking at ASP.NET MVC
After looking at another question on SO ( Using NaN in C++ ) I
After looking around (for not terribly long I have to admit) I wonder if
I have a query that is dynamically built after looking up a field list
I know this question has been asked a bit before. But looking around I
So, this question is similar to what I need, but the answers there don't
This question is more towards Design and Architecture and I want to know SO
After loosing much sleep I still cannot figure this out: The code below (its

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.