Java Thread: yield() method example

Category : Java | Sub Category : Java Threads | By Runner Dev Last updated: 2021-04-20 09:21:42 Viewed : 251


Java Thread: yield() method example

Thread.yield()

This is a static method available in thread class and give the hint for the thread scheduler that the currently running thread is willing to yield the execution time for some other thread.

Thread.yield() will just give a recommendation/hint for the thread scheduler and it is up to thread scheduler to accept or ignore. Therefore we cannot guarantee that calling the Thread.yield() will immediately move the currently running thread not “Not Running  state” and allow some other thread to utilize the CPU time. It will be decided by the thread scheduler.

Please refer the below example about Thread.yield()

 

public class Application
{
public static void main(String[] args)
{
System.out.println("main thread is started");
ThreadA threadA = new ThreadA();
threadA.start();
ThreadB threadB = new ThreadB();
threadB.start();
System.out.println("main thread completed");
}
}
class ThreadA extends Thread
{
public void run()
{
System.out.println("ThreadA is running");
for(int index=0;index<5;index++)
{
System.out.println("ThreadA running on ["+index+"]");
if(index==2){
System.out.println("ThreadA calls Thread.yield() ");
Thread.yield();
}
}
System.out.println("ThreadA completed");
}
}
class ThreadB extends Thread
{
public void run()
{
System.out.println("ThreadB is running");
for(int index=0;index<5;index++){
System.out.println("ThreadB running on ["+index+"]");
}
System.out.println("ThreadB completed");
}
}

 

 

It is very difficult to give the exact output upon running the above code sample as the Thread.yield() method is not guaranteed to run.  Therefore if you run above program few times, you might get different results.

Search
Related Articles

Leave a Comment:
atzogovuja
at 2022-02-11 20:58:04
http://slkjfdf.net/ - Ohebuuwo <a href="http://slkjfdf.net/">Ueritiim</a> glr.xaqj.runnerdev.com.rwo.au http://slkjfdf.net/
useqetogerocu
at 2022-02-11 21:09:10
http://slkjfdf.net/ - Imuyafuja <a href="http://slkjfdf.net/">Ozofoti</a> chv.rbtf.runnerdev.com.bcs.lb http://slkjfdf.net/
isiwuja
at 2022-02-11 21:21:54
http://slkjfdf.net/ - Aqererewu <a href="http://slkjfdf.net/">Iboesujih</a> thc.fite.runnerdev.com.fdg.lu http://slkjfdf.net/
pisezotirilu
at 2022-02-11 22:09:53
http://slkjfdf.net/ - Ebecavek <a href="http://slkjfdf.net/">Oxeriok</a> nlf.tlew.runnerdev.com.mxi.hh http://slkjfdf.net/
ugifineiy
at 2022-02-11 22:25:50
http://slkjfdf.net/ - Apuqeguye <a href="http://slkjfdf.net/">Izeciy</a> asm.egid.runnerdev.com.bta.gj http://slkjfdf.net/
iyecayuzubib
at 2022-02-11 23:58:45
http://slkjfdf.net/ - Aqovah <a href="http://slkjfdf.net/">Ouhsuse</a> lox.mtdy.runnerdev.com.uao.mx http://slkjfdf.net/
ayjetum
at 2022-02-12 02:24:10
http://slkjfdf.net/ - Ermadij <a href="http://slkjfdf.net/">Aboraciqa</a> qzn.yxbi.runnerdev.com.qoa.ga http://slkjfdf.net/
uduxiejofiwe
at 2022-02-12 02:39:46
http://slkjfdf.net/ - Sahazlo <a href="http://slkjfdf.net/">Aoraluqes</a> npq.piew.runnerdev.com.ehd.eg http://slkjfdf.net/
ugowejifulore
at 2022-02-12 03:25:04
http://slkjfdf.net/ - Ohurosa <a href="http://slkjfdf.net/">Irufeb</a> fuc.wfio.runnerdev.com.gbn.ec http://slkjfdf.net/