• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on Thread join() and yield() method

 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

Thread.yield();

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

thread1.yield();

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 ]
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see the code below is doesn't show anything special..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic