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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:48:28+00:00 2026-05-22T00:48:28+00:00

The JDK provides a set of thread-safe classes like ConcurrentHashMap, ConcurrentLinkedQueue and AtomicInteger. Are

  • 0

The JDK provides a set of thread-safe classes like ConcurrentHashMap, ConcurrentLinkedQueue and AtomicInteger.

Are these classes required to synchronize on this to implement their thread-safe behavior?

Provided that they do we can implement our own synchronized operations on these objects and mix them with the built-in ones?

In other words is it safe to do:

ConcurrentMap<Integer, Account> accounts 
    = new ConcurrentHashMap<Integer, Account>();

// Add an account atomically
synchronized(accounts) {
    if (!accounts.containsKey(id)) {
        Account account = new Account();
        accounts.put(id, account);
    }
}

And in another thread

// Access the object expecting it to synchronize(this){…} internally
accounts.get(id);

Note that the simple synchronized block above could probably be replaced by putIfAbsent() but I can see other cases where synchronizing on the object could be useful.

  • 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-22T00:48:28+00:00Added an answer on May 22, 2026 at 12:48 am

    Are these classes required to
    synchronize on this to implement their
    thread-safe behavior.

    No and, not only that, the various code inspection tools will warn you if you do try to use the object lock.

    In the case of the put method above, note the javadoc:

    A hash table supporting full
    concurrency of retrievals and
    adjustable expected concurrency for
    updates. This class obeys the same
    functional specification as Hashtable,
    and includes versions of methods
    corresponding to each method of
    Hashtable. However, even though all
    operations are thread-safe, retrieval
    operations do not entail locking, and
    there is not any support for locking
    the entire table in a way that
    prevents all access. This class is
    fully interoperable with Hashtable in
    programs that rely on its thread
    safety but not on its synchronization
    details.

    This means that the options are thread safe and there isn’t a way to do what you’re trying to do above (lock the whole table). Furthermore, for the operations that you use (put and get), neither of them will require such locking.

    I particularly like this quote from the javadoc from the values() method:

    The view’s iterator is a “weakly
    consistent” iterator that will never
    throw ConcurrentModificationException,
    and guarantees to traverse elements as
    they existed upon construction of the
    iterator, and may (but is not
    guaranteed to) reflect any
    modifications subsequent to
    construction.

    So, if you use this method, you’ll get a reasonable list: it will have the data as of the request time and might or might not have any later updates. The assurance that you won’t have to worry about the ConcurrentModificationExceptions is a huge one: you can write simple code without the synchronized block that you show above and know that things will just work.

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

Sidebar

Related Questions

Where to set the JDK to be used by the SunOne server on Solaris?
With JDK >= 1.5, should the preferred way to start a thread always be
JDK's concurrency package, Boost's thread library, Perl's Thread library (not in Python though) all
The JDK ships with CopyOnWrite* implementations for Set and List , but none for
JDK provides abillity to allocate so-called direct ByteBuffers, where memory is allocate outside of
In Java, with Sun's JDK 1.6, with an enum such as this: public enum
The JDK documentation for java.lang.String.hashCode() famously says: The hash code for a String object
i know that the JDK consists of all java packages .But what does the
Is there in the JDK or Jakarta Commons (or anywhere else) a method that
I want to install JDK 1.5 and 1.6 on XP, is it possible? how

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.