• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt on Thread join() and yield() method

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see the code below is doesn't show anything special..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic