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 ?
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.
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.
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%
Joined: May 17, 2006
see the code below is doesn't show anything special..