Does anyone know the differences between the methods acquire () and release () (java.util.concurrent.Semaphore) and await () and signal (new ReentrantLock().newCondition() ) .
Can you expose a pseudo code for each of these methods?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Superficially the behavior of these method might look similar –
acquire()/await()can make threads block in some cirsumstances andrelease()/signal()can unblock threads in some circumstances. However Semaphore and Condition serve different purposes:java.util.concurrent.Semaphoreis relatively higher-level synchronization mechanism, intended for use by general developers. You would use it typically to restrict concurrent access to some resource by making each requester thread callacquire()before accessing the resource (that way making the thread block if no semaphore permit was available).Description from the javadoc:
java.util.concurrent.locks.Conditionis relatively low-level synchronization mechanism which basically enhances functionality providedjava.lang.Objectmethodswait(),notify()andnotifyAll(). It enables the thread to suspend its activities when it needs to wait for some condition to become true (generally through activity of other threads) and then it enables those other threads to “wake up” the waiting thread(s) when the state variables taking part in the condition might have changed. It is generally harder to use correctly and general developers are advised to use higher-level mechanisms from package java.util.concurrent (like Semaphore).You can find more detailed information about this in the excellent book “Java Concurrency in Practice” from Brian Goetz.