Race Condition in Multithreaded Environment
What is Race Condition?
You can find many interviewers asking this question & many of them think that they know it.
So before understanding this with bookish words & mugging-up those things or wiring your mind as per the books, first just think about - What is race in sports or in general? What is the condition of that race?
If you understand that then have understod the answer, condition is more than one participants are running in race & all are capable.
So again think, if all equally capable participants are running in the race, then can you accurately predict the winner, NO.
In short, you can't predict the results of such race.
So 'Race Condition' in computer world is - Multiple capable threads are running to reach the common point with common goal, with common task. This common goal or task, you have written in run() of all the threads. So all threads are sharing & updating the state of that common task. But here, the situation is bit different than the actual race, as here all threads are basically fighting to update that common state & the thread which updates it in the last is winner, as its state will be considered for final calculation. And this behaviour makes the final result unpredictable & this can cause disaster in computer world.
Again 'Race condition' is simple, where many threads are running & updating the state of common task. But the unpredictable results & consequences of this race are centre of concern in programming world, and not the 'Race Condition' directly.
Below is one example to replicate this condition & the nature, in the end you can check the result showing the count of 3 possible results. So this way you can get any of these three possible results, as 3 threads are running here - Main, Race & Race1.
Note :- Please let me know if you have other thoughts on this. Don't start commenting on poor naming conventions, good
naming is not the aim of this sample here.
You can find many interviewers asking this question & many of them think that they know it.
So before understanding this with bookish words & mugging-up those things or wiring your mind as per the books, first just think about - What is race in sports or in general? What is the condition of that race?
If you understand that then have understod the answer, condition is more than one participants are running in race & all are capable.
So again think, if all equally capable participants are running in the race, then can you accurately predict the winner, NO.
In short, you can't predict the results of such race.
So 'Race Condition' in computer world is - Multiple capable threads are running to reach the common point with common goal, with common task. This common goal or task, you have written in run() of all the threads. So all threads are sharing & updating the state of that common task. But here, the situation is bit different than the actual race, as here all threads are basically fighting to update that common state & the thread which updates it in the last is winner, as its state will be considered for final calculation. And this behaviour makes the final result unpredictable & this can cause disaster in computer world.
Again 'Race condition' is simple, where many threads are running & updating the state of common task. But the unpredictable results & consequences of this race are centre of concern in programming world, and not the 'Race Condition' directly.
Below is one example to replicate this condition & the nature, in the end you can check the result showing the count of 3 possible results. So this way you can get any of these three possible results, as 3 threads are running here - Main, Race & Race1.
Note :- Please let me know if you have other thoughts on this. Don't start commenting on poor naming conventions, good
naming is not the aim of this sample here.
Below is the kind of output, where you can see the unpredictability of the above code where I have created 3 threads only, including the main program itself. So if the number of threads increases then you can assume the degree of unpredictability.