Difference between join() and yield() in Multithreading

Difference between join() and yield() in Tabular form

Join MethodYield Method
It ensures a calling thread to get terminated first and then after further execution is proceeded by other thread instanceIt simply makes other threads having same priority to do their task
It puts other threads to suspended state, till the thread on which the join() call is finishedIt moves a thread from running to runnable
It allows us to provide threshold timeIt doesn't, totally depends on thread Schedular
Priority doesn't matterApplies on same priority thread

Join() Method

Join method ensures termination or completion of a thread on which it is called. So, that other threads can join the calling thread as and when it dies.

The statement after the join() won’t be executed by current thread instance until the thread on which it is invoked finishes its task. However, it is also possible to mention maximum time to which extent to wait.

Java program Example:

class ThreadExample extends Thread{
    @Override
    public void run() {
        System.out.println("Child thread is printing");
       for(int i=0;i<10;i++)
       {
           System.out.println(i);
       }
        try {
            sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public class Main {

    public static void main(String[] args) throws InterruptedException {
ThreadExample thread1 = new ThreadExample();
thread1.start();

        thread1.join();
        System.out.println("I will wait for child thread to join me");
        System.out.println("Now Main thread is executing");
    }
}

Output

Child thread is printing
0
1
2
3
4
5
6
7
8
9
I will wait for child thread to join me
Now Main thread is executing

 

yield() Method

Yield method simply let other threads of same priority of the calling thread to finish their task.

In other words, It tells the thread scheduler that the thread who actually called the yield() is willing to wait and allow other threads having same priority to execute their task.

Java program Example:

class ThreadExample extends Thread{
    @Override
    public void run() {


       for(int i=0;i<10;i++)
       {
           yield();
           System.out.println("I am child "+Thread.currentThread().getPriority());
       }

    }
}
public class Main {

    public static void main(String[] args) {
ThreadExample thread1 = new ThreadExample();
thread1.start();

System.out.println(" Main thread is executing");
        for(int i=0;i<10;i++)
        {

            System.out.println("I am Main "+Thread.currentThread().getPriority());
        }
    }
}

Output:

Main thread is executing
 I am Main 5
 I am child 5
 I am Main 5
 I am child 5
 I am Main 5
 I am child 5
 I am Main 5
 I am child 5
 I am Main 5
 I am child 5
 I am Main 5
 I am child 5
 I am Main 5
 I am child 5
 I am Main 5
 I am child 5
 I am Main 5
 I am child 5
 I am Main 5
 I am child 5

 

Leave a Reply