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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:07:07+00:00 2026-06-11T14:07:07+00:00

I want to process a flow of client requests. Each request has its special

  • 0

I want to process a flow of client requests. Each request has its special type. First I need to initialize some data for that type, and after this I can start processing the requests. When the client type comes for the first time, I just initialize the corresponding data. After this all the following requests of that type are processed using that data.

I need to do this in a thread-safe manner.

Here is a code I have written. Is it thread-safe?

public class Test {

    private static Map<Integer, Object> clientTypesInitiated = new ConcurrentHashMap<Integer, Object>();

    /* to process client request we need to 
    create corresponding client type data.
    on the first signal we create that data, 
    on the second - we process the request*/

    void onClientRequestReceived(int clientTypeIndex) {
        if (clientTypesInitiated.put(clientTypeIndex, "") == null) {
            //new client type index arrived, this type was never processed
            //process data for that client type and put it into the map of types
            Object clientTypeData = createClientTypeData(clientTypeIndex);
            clientTypesInitiated.put(clientTypeIndex, clientTypeData);
        } else {
            //already existing index - we already have results and we can use them
            processClientUsingClientTypeData(clientTypesInitiated.get(clientTypeIndex));
        }
    }

    Object createClientTypeData(int clientIndex) {return new Object();}

    void processClientUsingClientTypeData(Object clientTypeData) {}
}

From one hand, ConcurrentHashMap cannot produce map.put(A,B) == null two times for the same A.
From the other hand, the assignment and comparisson operation is not thread-safe.

So is this code is ok?
If not, how can I fix it?

UPDATE:
I have accepted the answer of Martin Serrano because his code is thread-safe and it is not prone to double initialization issue. But I would like to note, that I have not found any isssues with my version, posted as an answer below, and my version does not require synchronization.

  • 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-11T14:07:09+00:00Added an answer on June 11, 2026 at 2:07 pm

    This code is not thread safe because

    //already existing index - we already have results and we can use them
    processClientUsingClientTypeData(clientTypesInitiated.get(clientTypeIndex));
    

    has a chance of getting the “” value you temporarily insert in the put check.

    This code could be made threadsafe thusly:

    public class Test {
    
        private static Map<Integer, Object> clientTypesInitiated = new ConcurrentHashMap<Integer, Object>();
    
        /* to process client request we need to 
           create corresponding client type data.
           on the first signal we create that data, 
           on the second - we process the request*/
    
    void onClientRequestReceived(int clientTypeIndex) {
        Object clientTypeData = clientTypesInitiated.get(clientTypeIndex);
        if (clientTypeData == null) {
            synchronized (clientTypesInitiated) {
              clientTypeData = clientTypesInitiated.get(clientTypeIndex);
              if (clientTypeData == null) {
                  //new client type index arrived, this type was never processed
                  //process data for that client type and put it into the map of types
                  clientTypeData = createClientTypeData(clientTypeIndex);
                  clientTypesInitiated.put(clientTypeIndex, clientTypeData);
              }
            }
        }
        processClientUsingClientTypeData(clientTypeData);
    }
    
    Object createClientTypeData(int clientIndex) {return new Object();}
    
    void processClientUsingClientTypeData(Object clientTypeData) {}
    

    }

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

Sidebar

Related Questions

I want to process a request header using a custom rewrite map. Therefore I
I have a single big text file in which I want to process each
I want to process MSXML which is generated by Visio 2010, Which Language will
I want to process large amount of small images on HDFS. Hadoop provides archive
I want to process every element of a for-loop. Taking this code, why is
I want to process a medium to large number of text snippets using a
I want to process a file line by line in c, all lines in
I have the following XML and I want to process it so that I
I have a c++ process, I want that process should always remain on foreground,
Say I have a data file that I want to process; I want to

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.