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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:26:08+00:00 2026-06-03T02:26:08+00:00

Based on the answers below : It is Still not clear to me: What

  • 0

Based on the answers below :
It is Still not clear to me: What does the synchronized construct on the concurrentMap do, if anything. i.e. in the case of a concurrentmap, what is the difference if any between synchronized(map) versus non synchronizing. I am not interested in the correctness or believed goodness of the solution. Just an answer to the question:
Q:What is the difference between synchronizing on a concurrentmap and not synchronizing on the same? Specifically with regard to performance.. will suffice. Nothing more please.
I am only interested in what happens and no supplementary advice.

I have a theoretical question which I am having a bit of a mental problem resolving:
Assume I have a Concurrent Collection class say=>ConcurrentHashMap map;

Assume I have three methods:

method1: synchronized(map){
       doSomethingWithThemap(); //Assume put integers 1.. 1000000
}

method2:doSomethingWithThemap(); //Note it is not synchronized 
method3:doSomethingElseWithThemap(); //Assume put integers 2000000.. 3000000

Now assume 2 TestCases:


  • TestCase1: Spawn two threads A & B. A calls method1 and B calls method3.
  • TestCase2: Spawn two threads A’ & B’. A’ calls method2 and B’ calls method3.

From a performance point of view I would expect TestCase2 to win, since from what I understand, in TestCase1 B cannot add to the map, albeit concurrent, since the synchronized block will hold the lock on the map, and this is not the case in TestCase2.

My Unit Tests DO NOT validate this hypothesis.

Q: What am I missing here. i.e. Given a synchronized block on a concurrentcollection is performance impacted at all?

  • 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-03T02:26:10+00:00Added an answer on June 3, 2026 at 2:26 am

    Q: What am I missing here.

    Your assumption that ConcurrentHashMap internally synchronizes on itself is not correct: according to the source code, the implementation uses java.util.concurrent.locks objects, instances of which are hidden inside the collection, so you cannot lock/synchronize on them.

    In general, this is a recommendation that writers of class libraries should follow: if you need to synchronize on an object, do not synchronize on this; create a private object inside your class, and synchronize on that object instead. Otherwise, you may be facing concurrency issues caused by others synchronized on your object, and holding the lock indefinitely.

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

Sidebar

Related Questions

EDIT, Changed the code slightly based on answers below, but still haven't got it
EDIT 2 Okay, based on the advice on the answers below I eliminated my
Based on some answers to this question it appears that +alloc does some behind-the-scenes
So my code is below. I'm not getting any errors and it places everything
MASSIVE EDIT BASED ON THE ANSWERS BELOW original post here http://webmasters.stackexchange.com before miration. I
UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
Update: Based on the answers I initially went the route of using IsInstanceOf() which
Update: Based on a couple of the answers I have received, I just want
UPDATE: Relative to the questions and answers below, it seems I have a misunderstanding
Does the copy constructor of any std::container (specifically std::queue) containing object pointers call the

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.