• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Concurrency and java.util.List

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have an ArrayList that is being used by multiple threads to save information. For efficiency sake, I would like to avoid synchronizing block of code whenever posible. Now, I guess ArrayList's add() method is not an atomic "operation" and it can be interrupted, but can that concurrent insertion of elements produce any unwanted concequences apart from maybe storing the information in a "strange" order. It would be really nice if anyone could help me out with this, or suggest a better way to store information from multiple threads in one "place".
Thanks,
Joe
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Highly *not* recommended. If you are lucky, you will lose data, meaning an entry might overwrite another entry. If you are not, internal counters will be incorrect, causing all sorts of problems.

Two points...

First, if you are using Java 5.0, look into either the ConcurrentHashMap, or the ConcurrentLinkedQueue. Neither one is a list, but both allow parallel reads, and sometimes parallel writes, and both are thread safe. And you might be able to use it with a small algorithm change.

And second, why?!?!? The operations are incredibly fast, mainly because they are incredibly simple. Isn't there better options to improving the speed of your code than to purposely try to live with race conditions?

Henry
 
Bojan Mihaljevic
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry. I read about the util.concurrent package, but I am using Java 1.4.2. Since you say there is no big performance overhead I will just use the synchronization technique. Thanks alot.

Joe
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe,

since you are using Java 1.4, you can use Doug Lea's concurrency package or, as recommended on the site, Dawid Kurzyniec's backport of the main java.util.concurrent classes (I couldn't get the second URL to work).

The java.util.concurrent package in Java 5 is taken largly from Professor Lea's package.
reply
    Bookmark Topic Watch Topic
  • New Topic