Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java hashmap access without exception

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Java hashmap access without exception

    Is it possible to access all values in a hashmap from another thread and changing the content at the same time without getting a java.util.ConcurrentModificationException?
    If yes how? I'm getting the exception in this line: Object[] objects = hashMap.values().toArray();


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,051
    My Mood
    Mellow
    Thanks
    184
    Thanked 650 Times in 637 Posts

    Default Re: Java hashmap access without exception

    "changing content at the same time" may be your problem. Access to the resource should be limited to one client at a time. Read up on java multithreading (or concurrency) and synchronized methods/statements and locks.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java hashmap access without exception

    I tried synchronised, but it made it too slow. Just one thread modifies the values, the other just read them.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,051
    My Mood
    Mellow
    Thanks
    184
    Thanked 650 Times in 637 Posts

    Default Re: Java hashmap access without exception

    That's not what your original problem description said, though there is no distinction between access for "read only" versus "read/write." Besides, what's the use of reading data that is changing?

    That adding synchronization "made it too slow" suggests you're not doing it right. Explore why one thread requires access to the data so long that it significantly slows down the program's performance. Then again, manipulating huge data stores can take time. How big is the map? Why are you converting the map values to an array? How often are you doing that?

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java hashmap access without exception

    I'm using the hashmap for a tile based game. I'm converting the map into an array because I update these tiles in an update thread.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,051
    My Mood
    Mellow
    Thanks
    184
    Thanked 650 Times in 637 Posts

    Default Re: Java hashmap access without exception

    Okay. Doesn't help me much, but I was hoping the questions I asked might give you some ideas where optimizations could occur. I suggest you use a profiler to find out what's taking so long.

Similar Threads

  1. how to access a hashmap which is inside a MultiMap
    By vidyasure in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 20th, 2012, 06:47 AM
  2. java.net.URL Access Exception
    By MistaWizurd in forum Java Networking
    Replies: 5
    Last Post: April 3rd, 2011, 02:18 AM
  3. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  4. no data found ms -access sql exception
    By jatinrai199 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 16th, 2011, 02:22 PM
  5. HashMap usage in Java
    By neo_2010 in forum Collections and Generics
    Replies: 2
    Last Post: September 18th, 2009, 02:12 AM