Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between condition variable and condition predicate in java synchronization

I am looking for an example with explanation to understand the difference between condition variable and condition predicate in java.

This is in the context of synchronization.

Also I would like to know whether these terms are actual java terms found in java documentations or other authors refer to these terms?

To clarify the question further, consider this example (taken from Java Concurrency book):

@ThreadSafe
public class BoundedBuffer<V> extends BaseBoundedBuffer<V> {

    public BoundedBuffer(int size) { super(size); }

    // BLOCKS-UNTIL: not-full
    public  synchronized  void put(V v) throws InterruptedException {
        while (isFull())
            wait();
        doPut(v);
        notifyAll();
    }

    // BLOCKS-UNTIL: not-empty
    public  synchronized  V take() throws InterruptedException {
        while (isEmpty())
            wait();
        V v = doTake();
        notifyAll();
        return v;
    } 
}

What is condition variable, what is condition predicate? You may have a better and simpler example than this to explain the difference. I am confused as what exactly each one is referring to (condition variable vs predicate), whether they are the same thing or not.

Someone understanding the low level of java concurrency is probably the best to answer this question.

like image 889
apadana Avatar asked Oct 21 '22 03:10

apadana


1 Answers

A condition variable is a construct provided by an OS or threading system that provides wait and notify operations and that maintains a set of waiting threads.

A condition predicate is a predicate (a boolean-valued function or expression) called or implemented by code that uses a condition variable. Briefly, a thread waits on a condition variable until the predicate is true, and a thread notifies (or signals) the condition variable when the predicate becomes true.

Put another way, a condition predicate is code that is evaluated to test something about the logical state of the object, whereas a condition variable is a mechanism to communicate among threads that are changing the object's state and that are waiting for the object to change state.

The example code is somewhat confusing because it uses two condition predicates with the same condition variable. A thread doing a put tests the isFull predicate (it doesn't have to be a function; it could be a Boolean expression) and a thread doing a take tests the isEmpty predicate. They both use the same condition variable, which is the one associated with this, the buffer object. Note that the condition tested in the while-loop is the inverse of the predicate. A thread doing a put operation waits until the predicate is true, so the code waits while the predicate is not true.

The term condition predicate doesn't appear to be standardized. It's a reasonably descriptive term, and Goetz uses it in Java Concurrency in Practice. In Lampson and Redell, Experience with Processes and Monitors in Mesa, they mostly just use the term predicate. (Java's object monitors are almost an exact copy of Mesa's. Pthreads' are also very similar.) I've also seen the terms precondition or state predicate used.

The term condition variable is a fairly standard term of art for this construct. It's used by Mesa and Pthreads and probably dates back to Hoare's original work with monitors. Oddly, Java specifications don't use this term very much; they simply refer to the monitor associated with each object and that it can be locked, unlocked, awaited, or notified. There is however a Condition interface and implementations in the java.util.concurrent.locks package. The Condition interface represents a condition variable.

like image 123
Stuart Marks Avatar answered Oct 22 '22 16:10

Stuart Marks