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.
 
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.
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top