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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:15:04+00:00 2026-06-04T04:15:04+00:00

From javadocs Map m = Collections.synchronizedMap(new HashMap()); … Set s = m.keySet(); // Needn’t

  • 0

From javadocs

Map m = Collections.synchronizedMap(new HashMap());
      ...
  Set s = m.keySet();  // Needn't be in synchronized block
      ...
  synchronized(m) {  // Synchronizing on m, not s!
      Iterator i = s.iterator(); // Must be in synchronized block
      while (i.hasNext())
          foo(i.next());
  }

Two queries:

1) Is Set returned by m.keySet() also a collection wrapper or just an unsynchronized set?

EDIT:

2)Is it necessary to synchronize on m in

 synchronized(m) {  // Synchronizing on m, not s!

Can’t we synchronize on s instead of m?

  • 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-04T04:15:05+00:00Added an answer on June 4, 2026 at 4:15 am

    1: Yes it returns a synchronized set that shares a mutex with the Map.

    2: Yes you need to hold the lock manually while iterating. If you don’t changes can be made in between calls to next() and you’ll still have problems. Remember it is part of the specification of HashMap that if another thread, for example does an m.put("foo", "bar"); in between two of your calls to i.next(), then next() will throw ConcurrentModificationException. To prevent this, you lock the whole map so nobody can change it until you’re done with the iterator. Locking just the set wouldn’t stop anybody from adding to the map.

    If you need to iterate while concurrent access may be happening, you should look at implementations of ConcurrentMap to make your life a lot easier.

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

Sidebar

Related Questions

From ImmutableList javadocs: Unlike Collections.unmodifiableList(java.util.List), which is a view of a separate collection that
The code pasted below was taken from Javadocs on HttpURLConnection . I get the
I need to get a collection from the java HashMap without the changes in
From the Quartz Scheduler javadocs for the method setTimeZone of class CronTrigger : If
To have browsable javadocs in a project hosted at Google-code, I got from here
Javadocs says When a key has been discarded its entry is effectively removed from
From the javadocs : public interface Cache<K,V> extends Function<K,V> { //... void invalidate(Object key);
From the Pattern javadocs: Greedy quantifiers: X? X, once or not at all X*
private final Map q; public Info() { this(Collections.EMPTY_MAP); } public Info(final Map q) {
From the JavaDocs: A ConcurrentLinkedQueue is an appropriate choice when many threads will share

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.