Example & Tutorial understanding programming in easy ways.

Whar is race condition in multi-threading in java?

Race condition in Java is a type of concurrency bug or issue which is introduced in your program because  parallel execution of your program by multiple threads at same time.

     Since Java is a multi-threaded programming language hence risk of Race condition is higher in Java which demands clear understanding of what causes a race condition and how to avoid that
. Anyway Race conditions are just one of hazards or risk presented by  use of multi-threading in Java just like deadlock in java.

Race conditions
occurs when two thread
operate on same object without proper synchronization and there operation interleaves on each other.

lassical example of Race condition is incrementing a counter since increment is not an atomic operation and can be further divided into three steps like read, update and write. if two threads tries to increment count at same time and if they read same value because of interleaving of read operation of one thread to update operation of another thread, one count will be lost when one thread overwrite increment done by other thread. atomic operations are not subject to race conditions because those operation cannot be interleaved.

Read More →