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
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.
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?