Slipped Condition is a special type of race condition that can occur in a multithreaded application. In this, a thread is suspended after reading a condition and before performing the activities related to it. It rarely occurs, however, one must look for it if the outcome is not as expected.
Example: Suppose there are two thread A and thread B which want to process a string S. Firstly, thread A is started, it checks if there are any more characters left to process, initially the whole string is available for processing, so the condition is true. Now, thread A is suspended and thread B starts. It again checks the condition, which evaluates to true and then processes the whole string S. Now, when thread A again starts execution, the string S is completely processed by this time and hence an error occurs. This is known as a slipped condition.
Program to demonstrate slipped conditions in Java
Characters left! I can process the string Thread2 trying to process the string Thread2 H Thread2 e Thread2 l Thread2 l Thread2 o No more character left to process. This is a slipped condition
The solution to the problem of Slipped Conditions is fairly simple and straightforward. Any resources that a thread is going to access after checking the condition, must be locked by the thread and should only be released after the work is performed by the thread. All the access must be synchronized.
With respect to the problem above, the slipped conditions can be eliminated, by locking the String object of the CommonResource class. In this scenario, the thread first gains the access and locks the String and then tries to process the String.
Solution to the above example
Characters left! I can process the string Thread2 trying to process the string Thread1 H Thread1 e Thread1 l Thread1 l Thread1 o
In the above program, a new static boolean member isLocked is added to the CommonResource class. Now, whenever a thread tries to process the string, it first obtains the lock and then processes it. In this example, the SlippedCondition thread checks whether the isLocked is false and there is string left to be processed. If it is, it obtains the lock and then waits and finally processes the string. Meanwhile, the ReadingThread tries to process the string but it can’t as isLocked was set true by the SlippedThread.
- Multithreading in Java
- Deadlock in Java Multithreading
- What does start() function do in multithreading in Java?
- Java Thread Priority in Multithreading
- Execute main() multiple times without using any other function or condition or recursion in Java
- CompositeName size() method in Java with Examples
- CompoundName getAll() method in Java with Examples
- CompositeName isEmpty() method in Java with Examples
- CompoundName compareTo() method in Java with Examples
- CompoundName startsWith() method in Java with Examples
- CompoundName remove() method in Java with Examples
- JapaneseChronology range() method in Java with Example
- TemporalAdjusters firstDayOfNextYear() method in Java with Examples
- CompositeName clone() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.