Java Concurrency Condition


Condition


A java.util.concurrent.locks.Condition interface provides a thread ability to suspend its execution, until the given condition is true.

We can Create Condition object by using ReentrantLock and ReentrantReadWriteLock Which are implementation classes of Lock interface. You can create condition variable by calling lock.newCondtion() method


Methods

·         await():The current thread suspends its execution until it is signalled or interrupted.

·         await(long time, TimeUnit unit) :The current thread suspends its execution until it is signalled, interrupted, or the specified amount of time elapses.

·         awaitNanos(long nanosTimeout) :The current thread suspends its execution until it is signalled, interrupted, or the specified amount of time elapses.

·         awaitUninterruptibly() :The current thread suspends its execution until it is signalled (cannot be interrupted).

·         await(long time, TimeUnit unit) :The current thread suspends its execution until it is signalled, interrupted, or the specified deadline elapses.

·         signal():This method wakes a thread waiting on this condition.

·         signalAll():This method wakes all threads waiting on this condition.



Condition variables are instance of java.util.concurrent.locks.Condition class, which provides inter thread communication methods similar to wait, notify and notifyAll e.g. await()signal() and signalAll().

if one thread is waiting on a condition by calling condition.await() then once that condition changes, second thread can call condition.signal() orcondition.signalAll() method to notify that its time to wake-up.



Locks are used for Sychronization.We will use Lock and Condition variables for solving classic Producer Consumer problem

1 Comments

Thank You

Previous Post Next Post