Difference between condition variable and condition predicate in java sync

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

This happens in the context of synchronization.

Also I would like to know if these terms are valid java terms found in the java docs or are other authors referring to these terms?

To clarify this question, consider this example (taken from the 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 a condition variable, what is a state predicate? You may have a simpler and simpler example than explaining this difference. I'm confused as to what exactly everyone means (condition variable vs predicate), whether they are the same or not.

Someone who understands the low level of java Concurrency would probably have a better answer to this question.

+3


source to share


3 answers


A state variable is a construct provided by the operating system or threading system that provides wait and notification operations and maintains a set of waiting threads.

A state predicate is a predicate (boolean function or expression) called or implemented by code that uses a condition variable. In short, a thread waits for a condition variable until the predicate is true, and the thread notifies (or signals) the condition variable when the predicate becomes true.

In other words, a state predicate is code that is evaluated to check for something about the logical state of an object, whereas a condition variable is a mechanism for exchanging data between threads that change the state of the object and that are waiting for the object to change state.



The code example is somewhat confusing because it uses two condition predicates with the same condition variable. The thread executing the put tests the predicate isFull

(it doesn't have to be a function, it can be a boolean expression), and the thread executing the test chases the predicate isEmpty

. They use the same condition variable associated with this

the buffer object. Note that the condition checked in the while-loop is the inverse of the predicate. The put thread waits until the predicate is true, so the code waits until the predicate is true.

The term condition predicate does not appear to be standardized. It's a reasonably descriptive term and Goetz uses it in Java Concurrency in practice. At Lampson and Redell, both process and monitor experience at Mesa, they mostly just use the term predicate. (Java Object Monitors are almost an exact copy of Mesa's. Pthreads' are very similar too.) I've also seen condition or state predicates used.

The term condition variable is a fairly standard term for this construct. It was used by Mesa and Pthreads and probably dates back to Hoare's original work with monitors. Oddly enough, the 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. However, there is an interface Condition

and an implementation in the package java.util.concurrent.locks

. The interface Condition

is a condition variable.

+2


source


A condition variable is a boolean value that tests one condition (can be a flag to wait for another task / thread).

Condition Predicates are lambda expressions, you can read about that here:



http://howtodoinjava.com/2014/04/04/how-to-use-predicate-in-java-8/

0


source


Variable condition - http://baptiste-wicht.com/posts/2010/09/java-concurrency-part-5-monitors-locks-and-conditions.html : A state variable is a kind of process queue awaiting the same condition

The state predicate is a really long link : The state predicate is a precondition that primarily determines the state of the operation

My interpretation: a state variable is a queue of processes that are waiting for a common condition predicate (which may or may not be wrong - again, my interpretation).

0


source







All Articles