Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

Difference between instance.wait() & wait()

Thread view: 
lonelyplanet999 - 05 Mar 2004 18:53 GMT
Hi,

I would like to know the difference in effect for calling wait() with
or without explicit object reference.

If o.wait() being called, I know that the calling thread is waiting on
object instance o. How about calling wait(), like below example ? :)

public class Test {
 public static void main(String argv[]) {
   TThread t1 = new TThread(), t2 = new TThread();
   t1.start();
   t2.start();
 }
}

class TThread extends Thread {
 public void run() {
   nowait();
 }
 public synchronized void nowait() {
   while (true) {
     try {
       wait();
     } catch (InterruptedException ie) {}
   }
 }
}
Steve Horsley - 05 Mar 2004 20:52 GMT
> Hi,
>
[quoted text clipped - 24 lines]
>   }
> }

Since TThread extends Thread, wait() ends up calling Thread's wait()
method, which waits for someone to notify itself.

I really do advise that you give up trying to extend Thread. It is
causing undue confusion.

Steve
lonelyplanet999 - 06 Mar 2004 06:33 GMT
> Since TThread extends Thread, wait() ends up calling Thread's wait()
> method, which waits for someone to notify itself.

You mean the thread instances t1 & t2 just wait on themselves ? If so,
who can wake them up ?

> I really do advise that you give up trying to extend Thread. It is
> causing undue confusion.
>
> Steve
Tony Morris - 06 Mar 2004 11:34 GMT
> Hi,
>
[quoted text clipped - 24 lines]
>   }
> }

Calling any method without a reference from a non-static context implies the
reference is 'this'.

In your case, wait() is equivalent to this.wait().
There is a deeper understanding (deeper than understanding the implicit
reference for a non-qualified method call for example) that is essential to
the use of wait/notify/notifyAll on thread monitors.

Signature

Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.