Java Multithreading Basics: Introduction to Thread.yield() Method

Thread.yield()method is a static method used to alert the scheduler that the current thread is willing to relinquish CPU resources to give other threads of the same or higher priority a chance to execute. Calling yield()the method does not guarantee that the current thread will stop executing immediately, but only means that the current thread is willing to give up a part of the execution time.

yield()The function of the method is to tell the scheduler that the execution priority of the current thread is low, and other threads with higher priority can be temporarily executed first. After calling yield()the method , the current thread will enter the ready state, waiting for the scheduler to reselect the execution thread.

There are a few things to note about the properties of yield()the method :

  1. yield()The method does not block the current thread, it just gives up part of the execution time to other threads, so the current thread is still in a runnable state and may continue to execute.

  2. yield()method does not release the lock held by the current thread, so other threads still cannot acquire the lock for execution.

  3. yield()The effect of the method depends on the implementation of the operating system and JVM, and different platforms may have different behaviors.

  4. yield()The use of the method needs to be cautious. Excessive use yield()may cause frequent switching between threads and reduce the overall performance of the system.

Here is a simple example to illustrate the use of yield()the method :

public class YieldExample {
    
    
    public static void main(String[] args) {
    
    
        Thread thread1 = new Thread(new MyRunnable(), "Thread 1");
        Thread thread2 = new Thread(new MyRunnable(), "Thread 2");

        thread1.start();
        thread2.start();
    }

    static class MyRunnable implements Runnable {
    
    
        @Override
        public void run() {
    
    
            for (int i = 0; i < 5; i++) {
    
    
                System.out.println(Thread.currentThread().getName() + ": " + i);
                // 当 i 等于 3 时,调用 yield() 方法让出 CPU 执行权
                if (i == 3) {
    
    
                    Thread.yield();
                }
            }
        }
    }
}

In the above example, we created two threads and started them. Each thread will perform the same task, which is to print the numbers from 0 to 4. When the thread executes to the number 3, the calling yield()method yields the CPU execution right. You'll notice in the output that threads may alternate execution because they voluntarily give up a portion of their execution time to other threads via yield()the method .

It is important to note that yield()methods make no guarantees about the order or frequency of thread switching. The exact thread scheduling behavior depends on the operating system and JVM implementation, so results may vary from system to system.

All in all, Thread.yield()the method is a way to prompt the scheduler, telling it that the current thread is willing to give up a part of the execution time to

other threads. However, it is not the primary means of achieving inter-thread cooperation and is used more for testing and debugging purposes. In most cases, it is more reliable and flexible to use more advanced concurrency tools such as locks, semaphores, condition variables, etc.

Guess you like

Origin blog.csdn.net/a772304419/article/details/131030629