Results 1 to 4 of 4
  1. #1
    otterslide is offline Member
    Join Date
    Sep 2016
    Posts
    2
    Rep Power
    0

    Default Is Re-assigning a Map in Multi-Threaded Safe?

    I have multiple threads accessing one Map that I want to refresh from the database. I re-load the values in a temporary map, then set it at the end:

    Java Code:
    Map usedMap = new HashMap(); 
    Map temporaryMap = new HashMap();
    // Loading temporary map code here.
    
    usedMap = temporaryMap; // Thread Safe?
    Would this be thread safe, or is it possible for a thread to access usedMap at a point that it has not had its reference fully set?
    Is there a better way to do this?

    Thanks.

  2. #2
    otterslide is offline Member
    Join Date
    Sep 2016
    Posts
    2
    Rep Power
    0

    Default Re: Is Re-assigning a Map in Multi-Threaded Safe?

    Ok, it looks like after some digging I found that I need "volatile" and that should work since assignment of reference is atomic.
    But I'm still wondering if there is a better way to do this?

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Is Re-assigning a Map in Multi-Threaded Safe?

    Instead of just swapping out a reference which is just as safe as blowdrying your hair in a bathtub, you might want to look into a producer-consumer pattern. Using locks (java.util.concurrent.locks package) you can decide who has access and who not. You can lock the map when a thread is writing to it, so those threads that read have to wait until the writing is done.

    Also take a look at the goodies you get in the java.util.concurrent package. See if java.util.concurrent.ConcurrentHashMap is what you want.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Is Re-assigning a Map in Multi-Threaded Safe?

    As reference assignments are atomic in nature you wouldn't need the volatile keyword since a thread cannot access that
    reference in an intermediate state. But there can still be complications while using a map reference that can be changed
    at unpredictable times. So some form of synchronization is warranted.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Problem with Multi-Threaded Socket IO
    By SirDagdlahan in forum Advanced Java
    Replies: 20
    Last Post: 06-14-2012, 03:08 AM
  2. JAVA web service (XML-RPC) is it multi threaded?
    By Rickyp in forum Advanced Java
    Replies: 1
    Last Post: 07-07-2011, 06:28 AM
  3. Implement a multi-threaded matrix multiplication program.
    By redasu in forum Threads and Synchronization
    Replies: 0
    Last Post: 11-17-2010, 09:00 AM
  4. Writing into a File in a Multi Threaded app.
    By snehalkgandhi in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-19-2008, 05:10 AM
  5. Replies: 0
    Last Post: 04-07-2008, 08:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •