-1

I'm starting to read Java and I'm in multithreading topic. Also I'm a C programmer, so I've knowledge on threading for C language.

I was searching for how threads are communicating with each other on specific object.

I found this question Java: How can the wait() and notify() methods be called on Objects that are not threads? but I've conflict.

What I know that synchronized keyword applies the concept of locking mechanism. So if I synchronized of an object and then called wait on this object does it mean that the lock will be freed for another thread to access? As the example in the previous link

Community
  • 1
  • 1
Mahmoud Emam
  • 1,499
  • 4
  • 20
  • 37
  • 1
    The wait will unlock and put the thread in a list waiting for a notify – Pascal Fares Feb 24 '16 at 06:53
  • 1
    wait/notify is the oldest and one of the lowest level APIs for multi-threading. It is hardly used any more. I suggest you use the newer concurrency APIs added in the last 11 years. I would start with Java 8 Streams and work backwards. – Peter Lawrey Feb 24 '16 at 09:01

4 Answers4

3

You say you're familiar with threading in C.

You can understand Java's behavior by imagining an implicit p_thread_mutex_t variable and an implicit pthread_cond_t variable associated with every Java object.

Suppose we have some object, foo. Let's use m(foo) to denote the imagined mutex, and let's use c(foo) to denote the imagined condition variable.

A synchronized(foo) { ... } block lock is almost the same as this:

pthread_mutex_lock(m(foo));
...
pthread_mutex_unlock(m(foo));

The only difference is, Java guarantees that there is no way to get out of the synchronized block without unlocking the mutex. Even if the ... throws an exception, the mutex still will be unlocked.

So, the answer to your question:

A call to foo.wait() basically translates to pthread_cond_wait(c(foo), m(foo));

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
2

When wait() is called, the lock on the synchronized object will be freed. See the below extract from Oracle documentation. The word "monitor" means as same as "lock"

" public final void wait() throws InterruptedException Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0). The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

 synchronized (obj) {
     while (<condition does not hold>)
         obj.wait();
     ... // Perform action appropriate to condition
 }

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

"

  • Re, _the word "monitor" means the same as "lock"_: Yeah, these days, I guess it does, but back when Java was invented, "monitor" was supposed to evoke C. A. R. Hoare's Monitors https://en.wikipedia.org/wiki/Monitor_%28synchronization%29 In that sense, a monitor was supposed to be an _object_ that had an associated mutex and one or more condition variables, and _every method_ of the object was supposed to synchronize on the mutex. – Solomon Slow Feb 24 '16 at 15:11
1

The general rules are as follows:

  • A thread can only call wait() or notify() when it acquired the lock synchronized

  • Two threads cannot run code within synchronized block simultaneously. The other threads have to wait to acquire the lock.

  • If a thread acquiring the lock called wait(), it frees the lock automatically. When it has been notify()ed, it attempts to acquire the lock again automatically.

-1

Take a look to the cubyhole "producer/consumer" example, it can help

http://www.tutorialspoint.com/javaexamples/thread_procon.htm

Pascal Fares
  • 21,959
  • 2
  • 14
  • 12
  • Providing just link to external resource is not considered a good answer http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – rkosegi Feb 24 '16 at 14:15