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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:15:06+00:00 2026-05-21T19:15:06+00:00

I need to synchrozize my accesses to the hashmap. Here are my options I

  • 0

I need to synchrozize my accesses to the hashmap.
Here are my options

  1. I know I can use Synchronize keyword. this is one option. Can I use the map itself ?
  2. currently I have a get method that if the object does not exists creates it and puts it in the map. I can synchronize the method.
  3. I can use a syncronize block
  4. I can use
    Map m = Collections.synchronizedMap(new HashMap(…));
    in my code.

I tend to do 4 cause it sounds the easiest. Any suggestions?

  • 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-21T19:15:07+00:00Added an answer on May 21, 2026 at 7:15 pm

    I would suggest that you don’t make methods synchronized, and that you don’t lock on the map itself. I generally prefer to use a separate locking object which is only used for locking and only known about in the class which owns the map.

    You could potentially use synchronizedMap, but it depends what you want to do with it. If you only ever get and put values, then that’s fine. If you ever need to iterate over the map, you need to block other threads from modifying the map while you’re iterating.

    Another option is to use a ConcurrentHashMap. See the docs for the semantics. This is probably the simplest approach if it behaves the way you need it to.

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

Sidebar

Related Questions

I need a very fast SQLite database acces. Setting parameters this way: PRAGMA synchronize
Need a map reduce function by mongo in php This my mongo structure [_id]
I know this is not a straight up question, so if you need me
I need to synchronize a path animation on two different layers (one is used
I need to use a HashMap of the form <String, ArrayList<String>> that is going
I use Task Parallel Library and I need to synchronize access to a method
I have a static Map, which I need to synchronize access to. The map
I need to synchronize two large sets of strings in Java, one of the
I just realized that I need to synchronize a significant amount of data collection
I need to have ALWAYS a background service that will synchronize my Android application

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.