πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

yield() Method in Java  


Introduction
  • The yield() method in Java is used to temporarily pause the currently executing thread and give a chance for other threads of the same or higher priority to execute.
  • When a thread calls Thread.yield(), it goes back to the Runnable state, so that the thread scheduler can decide which thread to run next.
  • Real-World Examples where yield() is used:
    • CPU-intensive tasks: When a long-running thread gives other threads a chance to execute.
    • Background tasks: Low-priority tasks can yield CPU to higher-priority tasks when needed.
    • Multithreaded games: To let multiple game entities (like players or enemies) get execution time fairly.
  • Java has provided the below yield() method in the Thread class:
    1. public static native void yield()
      • This is a native method, meaning its implementation is provided in another language (like C/C++) inside the JVM.
      • It is a static method, so it should be called as Thread.yield().
      • It does not guarantee that the current thread will stop; the decision depends on the thread scheduler.
  • Program:
    class MyThread extends Thread
    {
        @Override
        public void run()
        {
            // Child thread prints numbers from 1 to 5
            for (int i = 1; i <= 5; i++)
            {
                System.out.println(getName() + " is running: " + i);
            }
        }
    }
    
    public class YieldDemo
    {
        public static void main(String[] args)
        {
            System.out.println("Main thread started execution.");
    
            // Main thread creates another thread (child thread)
            MyThread t1 = new MyThread();
            t1.setName("Child-Thread");
            t1.start();
    
            // Yielding once in the main method
            // πŸ‘‰ Here the MAIN THREAD gives a chance (hint) to other threads of equal priority (Child-Thread)
            // If scheduler accepts the hint, Child-Thread will run first.
            Thread.yield();
    
            // Main thread continues its execution (printing 1 to 5)
            for (int i = 1; i <= 5; i++)
            {
                System.out.println("Main thread is running: " + i);
            }
    
            System.out.println("Main thread finished execution.");
        }
    }
  • Important Key Points about yield() method:
    • yield() is a static method of the Thread class.
    • It is used to pause the current thread temporarily and give a chance to other threads of equal priority to execute.
    • The call to yield() is only a hint to the thread scheduler; it is not guaranteed that the current thread will stop executing.
    • If no other thread of equal priority is waiting, the same thread may continue its execution.
    • It does not release any locks (monitors) held by the thread.
    • The thread moves from Running state back to Runnable state after calling yield().
    • yield() is declared as native, meaning its actual implementation is provided in another language (like C/C++) inside the JVM.
    • The behavior of yield() is platform-dependent and may vary across operating systems and JVM implementations.