• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Chapter 4- Condition Variables

 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Scott/Henry,

Could you please explain what Condition Variables are all about?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A condition variable adds to wait the ability to not wait when the condition you're waiting for has already taken place.




Using a condition variable to wait for events
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Bhat:
Hello Scott/Henry,

Could you please explain what Condition Variables are all about?



I am assuming you are asking about the condition variables that were added to JDK 1.5... Anyway, there is a short and long answer here.

The short answer: Condition variables are simply classes that implemented the wait() and notify() functionality.

The long answer: JDK 1.5 added a locking class (that implements the Lock interface). Basically this class provides explicit locking. There were multiple reason why it was added -- instead of just using the synchronized keyword.

First, is scope. With synchronized, the only scope available were at a block or method level. With an explicit locks, the scope can be multiple methods, it can be based on a user session, or data model, or any scope you want.

Second, is usage. You can have many locks in an object. You can share a lock among many objects, or any combinations you like.

Condition variables are the wait-and-notify counterpart to the lock class. If you use this new lock class, you need to use the new condition class.

Also, the condition variable is an independent object. A lock can have mulitple condition variables. A classic case is of a buffer. You need one lock to protect the buffer, but many need multiple condition variables; one when the buffer is empty waiting for data, one when the buffer is full waiting for space. Without separate condition variables, you typically have to use a notify all instead of just a notify on the specific condition variable.

Hope I didn't over answer this questions...

Henry
 
Pradeep bhatt
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry!

I am also reading your article here
http://www.onjava.com/lpt/a/5245
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic