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 6548931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:01:53+00:00 2026-05-25T12:01:53+00:00

In my work I stumbled upon such a design issue: I need one instance

  • 0

In my work I stumbled upon such a design issue:

  • I need one instance of a Manager class per thread
  • These instances should be globally accessible, like in the singleton pattern via a static function
  • Each thread might need to initialize its instance with different arguments
  • The lifetime of these instances should be controllable, sometimes it would be beneficiary to remove an instance and allow GC to collect it

The first two points would make it a ‘per thread singleton’ if such a thing exists.

This is what I came up with (the code is simplified, I’ve omitted safety checks and so on):

public class Manager {
  private final static ThreadLocal<Manager> local = new ThreadLocal<Manager>();

  private int x;
  Manager(int argument) { x = argument; }

  public static void start(int argument) { local.set(new Manager(argument); }
  public static void clean() { local.remove(); }

  private void doSomething1() { x++; .... }
  private int doSomething2() { if (--x == 0) clean(); ... }

  public static void function1() { local.get().doSomething1(); }
  public static int function2() { return local.get().doSomething2(); }
}

As you can see the clean function can be also called from within the private methods.
Also notice that through the use of static functions the reference to the instance is never leaked, so instances assigned to different threads won’t get mixed.

This works quite ok, but then I got another requirement:

  • Different threads may need to utilize different implementations of Manager class

So I defined an interface:

public interface ManagerHandler {
  void method1();
  int method2();
}

And modified the Manager class:

public class Manager {
  private final static ThreadLocal<ManagerHandler> local = new ThreadLocal<ManagerHandler>();

  public static void start(int argument) {
    ManagerHandler handler;
    // depending on the context initialize handler to whatever class it is necessary
    local.set(handler); 
  }
  public static void clean() { local.remove(); }

  public static void function1() { local.get().method1(); }
  public static int function2() { return local.get().method2(); }
}

An example implementation would look like this:

public class ExampleManagerImplementation implements ManagerHandler {
  private int x;
  public ExampleManagerImplementation(int argument) { x = argument; }
  public void method1() { x++; .... }
  public int method2() { if (--x == 0) Manager.clean(); ... }
}

Manager class works here as a facade, forwarding all the calls to the appropriate handler. There is one big issue with this approach: I need to define all the functions both in the Manager class and in the ManagerHandler interface. Unfurtunately Manager class can’t implement ManagerHandler interface, because it has static functions rather than methods.

The question is: can you think of a better/easier way to accomplish all the goals I’ve listed above that would be free of this issue?

  • 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-25T12:01:54+00:00Added an answer on May 25, 2026 at 12:01 pm

    There is not much you can do, as you basically need to proxy interface methods through static methods. I could only think of two ways to achieve the same functionality differently:

    1. If you’re using a DI framework, you can get rid of the static Manager and use an injected implementation of ManagerHandler which will contain the ThreadLocal.
    2. Generate (as in ‘bytecode generation’) the static ManagerAccess class using the methods found in the ManagerHandler interface.

    Personally, I wouldn’t think of having the static ManagerAccess class (which contains the ThreadLocal) around as a serious design issue. At least as long as it keeps to its own set of responsibilities (accessing thread-scoped instances and proxying calls) and doesn’t venture anywhere else.

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

Sidebar

Related Questions

I recently stumbled upon a global hotkey class ( This one ), it works
I stumbled upon a problem of how to make work together acts_as_taggable (on steroids)
I'm doing some Delphi (2010) work this summer, and I've stumbled upon this problem:
I'm hoping someone happens to have stumbled upon the following issue before. My Java
Lately I've stumbled upon an error in a lib that used to work just
I've stumbled upon the following two weird looking properties: Process.GetCurrentProcess().MainModule; Assembly.GetExecutingAssembly().Location; These properties are
Recently, I have stumbled upon the basic understanding about PKI work-in-action process. I have
At work, we stumbled upon Bugzilla creating HTML output that led to lines much
Today at work, I stumbled upon a problem that was driving me nuts. Basically
I stumbled upon a rather exotic c++ namespace problem: condensed example: extern C {

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.