A simple wait notify example

At times we often need to fetch an object which might take a long time. Our preferred way of doing that, especially when we are on a UI thread, is to spawn a different thread so as to keep the UI responsive (this is just one of the many use cases that I can think of now). But since we need that object to proceed further in the current execution, we have to resort to some sort of wait/notify mechanism. The following code demoes a very simplistic approach using the regular wait()/notify().

Note: This is far from fool proof. One case where it will fail is if the long task is over before that lock.wait() is called.

One thought on “A simple wait notify example

  1. Have you read Joshua Block, Effective Java, on finalizers?
    A good UI system should have callback mechanisms? Alternatively some lightweight JMS queue might be considered if it is not embedded development.

    Just blabbering here, no real expression of opinion 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *