Knock knock! Race Condition! Who’s there?
Race condition in Java (and ParaSail!)
This blog was prompted by a friend of mine asking about my t-shirt. If you know me beyond the screen, you’ll know I tend to wear a lot of nerdy tees. The one I was wearing that day was something like this:
In explaining the joke, I realized this would be an interesting topic of discussion on my blog. So, let’s get to it:
What is a race condition?
A race condition is when a system attempts to perform multiple operations at the same time, but because of the nature of the system, the operations must be done in the proper sequence to be done correctly.
A Practical Example:
Think about a simple light switch. In some homes, there are multiple light switches connected to a common ceiling light. When these types of circuits are used, the switch position becomes irrelevant. If the light is on, moving either switch from its current position turns the light off. Similarly, if the light is off, then moving either switch from its current position turns the light on.
With that in mind, imagine what might happen if two people tried to turn on the light using two different switches at exactly the same time. One instruction might cancel the other or the two actions might trip the circuit breaker.
How this works in Java:
Java, by nature, is multithreaded. This basically means that a single set of code can be used by several processors at different stages of execution.
In our example, we saw two different switches affecting the same source of light. This is an example of multithreading. Below, we can see a more detailed example, specific to computing.
In the above example, there are multiple threads being inputted into the Word Processor, which is being inputted into the CPU. You may notice race conditions when you are typing in a word processor or text editor. As you type, you may get a little red line under a word that is spelled correctly. This is a prime example of a race condition in a multithreaded system.
I am now a Multithreading Master. Time to see a race condition in action!
Multithreading is a powerful and useful way to implement code. It’s one of the reasons Java is preferred among many software engineers. However, as every rose has its thorns, powerful processes have its bugs.
Race Condition Example:
This instance variable c is incremented and decremented so every thread should leave it in the state it initially was i.e. if c is zero in the start, incrementing it will make it 1 and decrementing it will make it zero again.
When you run this code, the variable c will output different answers with each thread.
Race Condition is usually bad. Usually.
There is an instance where race conditions can actually be a good thing. Reading a blog on ParaSail, tentatively named for “Parallel, Specification and Implementation Language,” it is a “new programming language designed to allow productive development of parallel, high-integrity (safety-critical, high-security) software systems.”
In the blog, written by Tucker Taft, it discusses the “intentional race condition in a ParaSail concurrent loop.”
Taft explains that this would seem to be a “relatively common paradigm when doing a parallel search, where you want to stop as soon as the item of interest is found in the data structure being walked, for example, and there is no need for the other threads to continue working.”
Race conditions can be dangerous, but can also be used for good.
Did I write this entire blog post to explain a joke? Perhaps. But, I bet you learned something along the way.
Sources:
If you want to buy the shirt (now that you can explain it to all your friends), click here.