aspose file tools*
The moose likes Threads and Synchronization and the fly likes How to avoid race conditions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to avoid race conditions?" Watch "How to avoid race conditions?" New topic
Author

How to avoid race conditions?

azuki ooh
Greenhorn

Joined: Nov 07, 2008
Posts: 20
Can synchronizing Threads prevent race condition?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Done correctly, yes. But you have to design the synchronization carefully. Improper or over use can lead to deadlocks, and it is easy to synchronize on the wrong thing (thus making it appear to be safe, but not protecting the data correctly), or under synchronize (for example, because you are using a List whose methods are all synchronized, you expect it to be safe to iterate over the list - which it is not.)

So you have to think and design your synchronization. Just putting synchronized blocks/methods is not enough.


Steve
Vish Shukla
Ranch Hand

Joined: Oct 12, 2008
Posts: 111
Hi Azuki,

Use appropriate synchronization in below sequence of preference
1. Reentrant read-write lock
2. Synchronized block
3. Synchronized method

Also, you can try actor based concurrency. This is a kind of pattern where you dedicate ONLY ONE thread to do mutations on a resource.

To avoid race conditions, the answer is religious code review, multi-threaded unit tests. There is no short cut.

The best thing would be to create side-effect free and stateless functions, use immutables as much as possible. But that is not possible always. So use java.util.concurrent.atomic, concurrent data structures like prefer ConcurrentHashMap instead of HashMap.

The best resource for concurrency is JCIP. You can also get some more details on above explanation here.


Thanks & Regards,
Vishal S Shukla (SCJP 93%, SCWCD 94%, SCBCD 100%)
Stefano Carniel
Greenhorn

Joined: Aug 01, 2013
Posts: 7

I suggest to read the Concurrency chapter in Thinking in Java (available for free). There is a wide section dedicated to race conditions and deadlock
Thomas Krieger
Greenhorn

Joined: Oct 28, 2013
Posts: 2
Wikipedia says a race condition
is the behavior of an electronic or software system where the output is dependent on the sequence or timing of other uncontrollable events. It becomes a bug when events do not happen in the order the programmer intended.


In java first avoid access to shared field without one of the following synchronization actions, see java memory model for a detailed explanation: volatile field, synchronized statement, java.util.concurrent.locks or java.util.concurrent.atomic. The following class will give results dependent on how many cores your pc has:

Since each cpu core caches the data from main memory the result depends on how many threads run on the same core as the main thread. By using volatile, each read or write to the volatile field will flash the cache. In the example given above, this will not be enough since the block:

must be atomar, so that the result is independent of the scheduling of the threads. This can be done by putting an synchronized block around it.

To detect race conditions, especially the first type of error, I implement the tool vmlens: Find Java race conditions automatically.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to avoid race conditions?