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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:33:22+00:00 2026-06-17T08:33:22+00:00

I have this singleton that creates objects, currently it looks like this: public ApplicationManagerSingleton

  • 0

I have this singleton that creates objects, currently it looks like this:

public ApplicationManagerSingleton {
    ....
    private Map<String, Thing> map = new HashMap<String, Thing>();

    public Thing getThingById( String id ) {
       Thing t = null;
       if ( !map.contains(id) ) {
        t = longAndCostlyInitializationOfThing();
        map.put(id, t );
       }
       return map.get(id);
     }
 }

The obvious problem it has is, if two threads try to access the same thing, they may endup duplicating the thing.

So I used a lock:

 public ApplicationManagerSingleton {
      private Map<String, Thing> map = new HashMap<Sring, Thing>();
      public Thing getThingById(String id ) {
          synchronized( map ) {
             if (!map.contains(id)) {
                 t = initialize....
             }
             map.put(id, t);
           }
           returns map.get(id);
      }
 }

But now that’s worst because I’ll be locking the map for a while each time a new resource is being created in demerit of the other threads wanting different things.

I’m pretty sure it can be better with Java 5 concurrent package. Can somebody point me in the right direction?

What I want to avoid is to lock the class or the map for other threads that are interested in other things.

  • 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-17T08:33:23+00:00Added an answer on June 17, 2026 at 8:33 am

    I tried these solutions and they failed at some point in my implementation, not saying they wont work in other scenarios.

    What I finally end up doing was to use a ConcurrentMap to check if the resource has been already requested or not. If not, it is created and stored somewhere else.

    ... 
    import java.util.concurrent.ConcurrentMap;
    import java.util.concurrent.ConcurrentHashMap;
    
    ... 
    private ConcurrentMap<String, Boolean> created = new ConcurrentMap<>();
    ....
    if ( created.putIfAbsent( id, Boolean.TRUE ) == null ) {
        somewhereElse.put( id, createThing() );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have this class: @Singleton public class Parent { ... }
I have a helper class that creates some objects, like a builder. The helper
I have a class that is a singleton. This singleton is instantiated using GCD
I have a singleton implemented like this: class Test123(object): _instance = None def __new__(cls,
I have two singletons, the first one's header looks like this (I've omitted everything
So I have a Singleton class and in it a non-static public method that
I have a singleton class for global access to config information. This singleton class
Looking at this article: http://www.galloway.me.uk/tutorials/singleton-classes/ I have implemented a singleton, my question is the
I have a question, is this the correct approach to make a Generic Singleton?
I have this string 2012-06-27 16:17:06 and I want to convert it to GMT

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.