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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Multithreading in Java
- Deadlock in Java Multithreading
- MultiThreading in Android with Examples
- Java Thread Priority in Multithreading
- What does start() function do in multithreading in Java?
- Execute main() multiple times without using any other function or condition or recursion in Java
- Top 5 Open Source Java Frameworks in 2020
- Android Listview in Java with Example
- How to populate RecyclerView with Firebase data using FirebaseUI in Android Studio
- Android Animations using Java
- How to create an Expandable CardView in Android
- How to add Custom Fonts in Android
- Swap the first and last character of a string in Java
- Cryptographic Hash Function in Java
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.