Threads and synchronized methods


L

Luca D.

Let's suppose to have two threads, A and B, which use an object C:

public class C {
public synchronized void f1() {
while(true) {
wait();
}
}

public synchronized void f2() {
notifyAll();
}
}

If A and B have THE SAME reference to the object C, and A calls f1(),
can B call the mothod f2 to wake up A, or the entire object C is
blocked because of the waiting thread (A)?
Would it make any difference if A and B had two different instances of
C?
Thanks.
 
Ad

Advertisements

J

Joshua Cranmer

Luca said:
If A and B have THE SAME reference to the object C, and A calls f1(),
can B call the mothod f2 to wake up A, or the entire object C is
blocked because of the waiting thread (A)?

Read the documentation for the wait and notify* methods:

wait():
Causes the current thread to wait until either another thread invokes
the notify() method or the notifyAll() method for this object, or a
specified amount of time has elapsed.

The current thread must own this object's monitor.

This method causes the current thread (call it T) to place itself in the
wait set for this object and then to relinquish any and all
synchronization claims on this object.

[ Note: if you call x.wait(), you must be synchronized on x for it to
succeed, otherwise you get an IllegalMonitorStateException ]
Would it make any difference if A and B had two different instances of
C?

Um... yes. You would be using two different objects for synchronization...
 
C

coffeymex

If A and B have THE SAME reference to the object C, and A calls f1(),
can B call the mothod f2 to wake up A, or the entire object C is
blocked because of the waiting thread (A)?

A thread releases the lock to the object it is synchonising one while
it is waiting (and when it is woken, it re-acquires the lock before
being
able to continue).

In case it's useful, some other information/examples I've written
on wait/notify:

http://www.javamex.com/tutorials/synchronization_wait_notify.shtml.

Note that in most cases, it's really worth considering using one of
the Java 5 concurrency utilities instead of wait/notify.

Neil
 
M

Mark Space

Luca said:
If A and B have THE SAME reference to the object C, and A calls f1(),
can B call the mothod f2 to wake up A, or the entire object C is
blocked because of the waiting thread (A)?
Would it make any difference if A and B had two different instances of
C?


Does this look like a homework problem to anyone else?
 
L

Lew

Mark said:
Does this look like a homework problem to anyone else?

I sure hope not, because the way the question is phrased, that would
mean someone is teaching Java who totally doesn't understand it.
 
Ad

Advertisements

M

Mark Rafn

Luca D. said:
public class C {
public synchronized void f1() {
while(true) { wait(); }
}
public synchronized void f2() { notifyAll(); }
}
If A and B have THE SAME reference to the object C, and A calls f1(),
can B call the mothod f2 to wake up A, or the entire object C is
blocked because of the waiting thread (A)?

Nope. wait() releases the lock before waiting. When notified, it continues
to be blocked until it can re-acquire the lock before continuing.
Would it make any difference if A and B had two different instances of
C?

Obviously, as this.wait() and this.notifyAll() on two different objects refer
to different 'this'es, so won't interact with each other at all.
 
Ad

Advertisements


Top