Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Threads and synchronized methods

Reply
Thread Tools

Threads and synchronized methods

 
 
Luca D.
Guest
Posts: n/a
 
      04-30-2009
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.
 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      04-30-2009
Luca D. wrote:
> 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...

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
 
 
 
coffeymex
Guest
Posts: n/a
 
      04-30-2009
> 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/syn...t_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
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      04-30-2009
Luca D. wrote:

> 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?
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-30-2009
Luca D. wrote:
>> 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?


Mark Space wrote:
> 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.

--
Lew
 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      04-30-2009
Luca D. <(E-Mail Removed)> wrote:
>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.
--
Mark Rafn http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
data shared between threads and synchronized on different objects Marcin Rodzik Java 6 08-25-2011 04:58 PM
Synchronized Block v.s. Synchronized Method Jerry Java 4 08-11-2010 02:34 PM
Question on synchronized and non synchronized methods ankur Java 4 11-28-2008 09:56 PM
synchronized block in synchronized static method dmcreyno Java 9 06-27-2006 07:43 PM
Use of synchronized variables over synchronized methods? Pep Java 6 08-15-2005 01:29 PM



Advertisments