1

When we are using wait and notify in thread environment. I have a class to process data as background process. And when there is no data to process it should call wait.

synchronized(some_object){
   wait();
}

In another class I am adding the data again. I need call notify() method.

synchronized(some_object){
   runnabale_object.notify();
}

Why i should use same object for synchronized block in those two different class. As i read synchronize is used to

The "Synchronized" keywords prevents concurrent access to a block of code or object by multiple Threads.

But these two are different block. But i can understand the problem when we use multiple threads. While one thread block other can call notify before the same thread call notify.

My Questions

  • Can we use different lock objects (synchronized(object)) for single threaded environment?
  • Best way of use same lock object when we have wait and notify in different classes?
2
  • You have to call wait and notify on the lock object (in your case some_object.wait() and some_object.notify())! Commented Aug 13, 2015 at 7:18
  • Your code doesn't really work as it is, from the Javadoc The current thread must own this object's monitor. (emphasis mine). So if you want to use wait/notify then the waiting/notifying Thread must own the monitor of the Object it is operating on. This also answers your question, you must use the same object for synchronizing as for wait/notify. Commented Aug 13, 2015 at 7:18

3 Answers 3

1

Can we use different lock objects (synchronized(object)) for single threaded environment?

In a single threaded environment, you don't need the locks. You can use anything you want or nothing at all.

In a single threaded environment you can guarantee no thread is wait()ing so the notify() will not do anything.

Best way of use same lock object when we have wait and notify in different classes?

When you notify(), you must perform a state change. When you wait() in a loop you much check for that state change. If you don't do this you can have two problem.

  • the notify() is lost
  • the wait() wakes spuriously, ie no notify.
Sign up to request clarification or add additional context in comments.

3 Comments

in my case as i mentioned a single thread processing data, when there is no data. i have to hold the process until new data comes in. So my approach of wait and notify is wrong?
Sorry i'm confused. Here single thread environment means i am going to create a single thread. So main thread can notify the created thread right. obviously it is multi threaded. rt?
@Exbury the change you need to make is that the wait() loop should check whether you really have more data. I assume you will only call notify() after you have added data. Any common data structure should be updated/read inside the synchronized block as well. BTW having one thread writing and one thread reading is still a multi-threaded environment.
0

when there is no data to process it should call wait.

Not when, but while.

Wait and notify are low-level primitive operations that are meant to be used in a very specific way.

In a consumer thread:

synchronized(lock) {
    while (thereIsNothingToProcess()) {
        lock.wait();
    }
    processSomething();
}

In a producer thread:

synchronized(lock) {
    makeSomethingAvailableToProcess();
    lock.notifyAll();    //or lock.notify() if you think you can get away with it.
}

If you follow this exact pattern, then:

  • You will not run into trouble when multiple consumers are competing for the same thing-to-be-processed,
  • You will not run into trouble because of spurious wakeups, and
  • You will not run into trouble because of the lost notification problem.

Comments

0

I've had the same question so I looked it up. The reason that two synchronized block can be used in the same object, is that 'wait()' will actually release the monitor so that other thread can obtain the guardian of the same object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.