Difference between Yield and Join Method in Java with Example

Multi-threading is a hot topic among investment banks interview. Difference between yield and join method is more of a theoretical question. I have already discussed difference between sleep and wait method in java.

Read Also :  Difference between Wait and Sleep method in Java


Difference between Yield and Join method in Java with Example


1.  Purpose : Yield in english means to give up or to surrender. Yield means currently executing thread gives chance to the threads that have equal priority in the Thread-pool.Yield does not guarantee that it will change the state of the currently executing thread to runnable state immediately.
If a join is called on a thread instance than it can be used to join the start of a thread's execution to the end of another thread's execution, such that a thread will not start running until another thread has ended.

2. State change :  Yield changes the state from running to runnable. If the method join() called on the
Thread instance, a thread will not start running until another thread finish executing.

3.  Keywords used in the method :    According to Oracle docs ,Yield method is represented as

public static native void yield()

Thus , static and native are the keywords used in the yield method.

Join method is represented as

public final void join()

Thus, final is the only keyword used in the join method.

Example of Yield and Join method in Java

       

public class YieldExample
{
   public static void main(String[] args)
   {
      Thread producer = new Producer();
      Thread consumer = new Consumer();
       
      producer.setPriority(Thread.MIN_PRIORITY); //Min Priority
      consumer.setPriority(Thread.MAX_PRIORITY); //Max Priority
       
      producer.start();
      consumer.start();
   }
}
 
class Producer extends Thread
{
   public void run()
   {
      for (int i = 0; i < 3; i++)
      {
         System.out.println("Producer : Produced Item " + i);
         Thread.yield();
      }
   }
}
 
class Consumer extends Thread
{
   public void run()
   {
      for (int i = 0; i < 3; i++)
      {
         System.out.println("Consumer : Consumed Item " + i);
         Thread.yield();
      }
   }
}

Output of above program without yield:
Consumer : Consumed Item 0
Consumer : Consumed Item 1
Consumer : Consumed Item 2
Producer : Produced Item 0
Producer : Produced Item 1
Producer : Produced Item 2

Output of above program with yield:
Producer : Produced Item 0
Consumer : Consumed Item 0
Producer : Produced Item 1
Consumer : Consumed Item 1
Producer : Produced Item 2
Consumer : Consumed Item 2



public class JoinExample
{
   public static void main(String[] args) throws InterruptedException
   {
      Thread t = new Thread(new Runnable()
         {
            public void run()
            {
               System.out.println("First task started");
               System.out.println("Sleeping for 2 seconds");
               try
               {
                  Thread.sleep(2000);
               } catch (InterruptedException e)
               {
                  e.printStackTrace();
               }
               System.out.println("First task completed");
            }
         });
      Thread t1 = new Thread(new Runnable()
         {
            public void run()
            {
               System.out.println("Second task completed");
            }
         });
      t.start(); // Line 15
      t.join(); // Line 16
      t1.start();
   }
}
 
Output:
 
First task started
Sleeping for 2 seconds
First task completed
Second task completed


Similarities between Yield and Join method in Java

1. Thread class :  Both the methods yield and join belongs to java.lang.Thread class.

2. State :   Both the methods can change the state of the currently executing thread but not guaranteed.



Recap : Difference between Yield and Join method in Java






YieldJoin
Purpose 
Yield means currently executing
thread gives chance to the threads
that have equal priority in the
Thread-pool.
A thread will not start running until
another thread has ended

State Changerunning to runnable If the method join() called on
the Thread instance, a thread
will not start running until another
thread finish executing.
Keywords Usedstatic,nativefinal



If you know any other difference between yield and join method in java then please mention in comments.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry