Thread yield() method in Java with Example

In this post, we will see what is Thread yield() method in Java. Let’s see some points related to the yield() method.

Thread yield() method in Java

  • Method declaration – public static native void yield().
  • Since this method has been declared as native there is no implementation logic in java.
  • The implementation of yield() method has been given in the C  language.
  • What it does – It pause execution of current thread and give chance to execute another thread. Suppose we have two thread t1 and main and both are executing simultaneously if we call Thread.yield() method for t1 then we are saying hey t1 just wait for some time, let execute the main thread. But calling yield() method doesn’t guarantee that it will call other thread. It depends on the Operating System. Is it confusing? Don’t worry we will see later in the example.

Thread yield() method in Java Examples.

Example – First we will see a general example without using Thread.yield(). We will create one thread extending Thread class and other thread we will have the main thread, something like below.

Output – Output may vary since both threads will execute simultaneously. Some of possible out is as below.

Thread yield() method in Java

Another possible output –

Another possible output –

Thread yield() method in Java

 

These are some possible output, you may get the same output but you need to run multiple time then the output will differ. Now we will use yield() method with thread t1 and see what happens.

 

What is expected output here? As we discussed in the beginning since we are calling Thread.yeild() for t1, t1 will request to operating system hey can you ask main thread to execute.  So the expected output is –

Thread1 created with name: – Thread-0
Main created with name: – main
Main created with name: – main
Main created with name: – main
Main created with name: – main
Main created with name: – main
Thread1 created with name: – Thread-0
Thread1 created with name: – Thread-0
Thread1 created with name: – Thread-0
Thread1 created with name: – Thread-0

 

But if you run the above program you mostly get a different output.

you may get –

Main created with name: – main
Main created with name: – main
Main created with name: – main
Main created with name: – main
Main created with name: – main
Thread1 created with name: – Thread-0
Thread1 created with name: – Thread-0
Thread1 created with name: – Thread-0
Thread1 created with name: – Thread-0
Thread1 created with name: – Thread-0

Thread yield() method in Java

 

Or you may get –

 

The thread t1 and main is still executing in a different sequence. Seems yield() is not working properly. It’s a little bit confusing now. Isn’t it? Let’s see what going on here. Don’t forget yield() doesn’t give guarantee to execute other thread(as of now main thread). It depends on the operating system and other factors, which thread is going to execute first.

Now it may question come in mind or you may encounter in the interview when you will use yield() method. In real-time we generally avoid the yield() method. We have other options than yield() like sleep() and join(). We can use these methods.

 

Summary –

The yield() method gives the chance to execute other thread but doesn’t give a guarantee. Calling yield() method, just we say to the operating system can you give chance to execute other thread. But it depends on the operating system it gives chance to other thread or not.

In real-time development generally, we don’t use yield() method, instead we can go for sleep(), join() etc.

That’s all about Thread yield() method in Java.

yield() method docs.

Top