File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt  on Thread join() and yield() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt  on Thread join() and yield() method" Watch "doubt  on Thread join() and yield() method" New topic

doubt on Thread join() and yield() method

ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
Just now i have go through some book regarding the join()and yield() method..but i couldn't see any effect of yield() method and join() method..i know what they are use for but i just couldn't see their effect..can someone show me the effect of both of the method ?
Douglas Chorpita
Ranch Hand

Joined: May 09, 2006
Posts: 97
When dealing with threads it is important to differentiate the current thread (which is running the code) and a reference to a Thread object (which creates a JVM thread that may run some other code). I often use the terms "current thread" and "reference thread".

yield() is a static method. Static methods can be invoked with a class name or with an instance of an object.

We can say:


But, if we have a reference to a thread, call it "thread1", we can also say:


This leads to confusion, as it is not the "reference thread" that "yields". It is always the "current thread". Why? Well, for one thing, it is a static method and doesn't even know about the "reference thread". The fact that we can call it with "thread1" is just a part of Java syntax that allows us to invoke static methods with references.

The "current thread" is the thread that yields. What does yield() do? It is not clear and can vary from one JVM to another. The JVM can keep the current thread in the running state or it can move it to the runnable state (giving other threads a chance to run). We can't be sure which will happen.

Now, join().

join() is an instance method. It is called with a reference to a Thread object. join() forces the "current thread" to wait until the "reference thread" is finished (enters the dead state). It essentially takes two threads and "glues them together". It stops "the current" (Thread A), runs the "reference thread" (Thread B) until it is done, and then goes back and continues with the thread that was the "current thread" (Thread A). If, at the time we call join(), Thread B is already dead, then the "current thread" keeps running.

Got it?

What else?

Important facts:

yield() is a static method.

join() is an instance method and must be called in a try-catch-block which tests for an InterruptedException.
[ July 27, 2006: Message edited by: Douglas Chorpita ]

SCJP 1.4 - 95%
ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
see the code below is doesn't show anything special..
I agree. Here's the link:
subject: doubt on Thread join() and yield() method
It's not a secret anymore!