Thursday, June 23, 2016

Race Condition in Java

Many of us might be aware of Synchronization in Multi-threading environments in java. 
Race condition in Java is a type of concurrency error 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. 

To Find Race conditions is very difficult, though since readability of Java code is very good and synchronized constructs are well defined heaps to find race conditions by code review. One can not find the race condition at the time of unit testing. Because race condition is the situation that won't occur often.  You can find the race condition in your program by reviewing it manually or you can use best code review tools available in the marketplace(try to find one such tool and let me know if you found that :).


Mostly it is reviewed that the collections like HashMap must be used with very much care as this is non-synchronized and multiple threads can apply for the re-size operation at the same time. Hence, it is recommended for you that if you are working in the multi-threaded application then use ConcurrentHashMap or use the synchronized HashMap which is again not recommended because of low performance in the multi-threaded environment.



No comments:

Post a Comment