Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Java Language
Expertise: Beginner
Jul 14, 1999



Application Security Testing: An Integral Part of DevOps

Conditional Semaphores

I am trying to create a queue that will be emptied by multiple threads simultaneously. In C/C++ this requires the use of a conditional semaphore to signal that the queue is empty/not empty. How can a Condition be implemented in Java?

Conditions are implemented in Java using the wait() and notify() methods defined in java.lang.Object. Just create an arbitrary object, such as an integer array of length one, and use that as your condition. To wait for condition, invoke its wait() method, and to signal a condition, invoke its notify() or notifyAll() method depending on how many waiting threads you want to wake up. The thing to remember is that you must own the object's monitor before calling either method, which requires a synchronized block.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date