concept

We know that the start() method starts the thread and makes it ready for execution when the CPU schedules it.

So what does the yield() method do? Look at the source code.

/**
 * A hint to the scheduler that the current thread is willing to yield
 * its current use of a processor. The scheduler is free to ignore this
 * hint.
 *
 * <p> Yield is a heuristic attempt to improve relative progression
 * between threads that would otherwise over-utilise a CPU. Its use
 * should be combined with detailed profiling and benchmarking to
 * ensure that it actually has the desired effect.
 *
 * <p> It is rarely appropriate to use this method. It may be useful
 * for debugging or testing purposes, where it may help to reproduce
 * bugs due to race conditions. It may also be useful when designing
 * concurrency control constructs such as the ones in the
 * {@link java.util.concurrent.locks} package.
 */
public static native void yield();
Copy the code

Yield, which means “humility,” is also a method of Thread. It lets the current thread CPU time slice, causes the running thread to return to the ready state, and recontest the CPU scheduling rights. It can be picked up, or it can be picked up by another thread.

In actual combat

Here is an example of how to use it.

Public static void main(String[] args) {Runnable Runnable = () -> {for (int i = 0; i <= 100; i++) {
			System.out.println(Thread.currentThread().getName() + "-- -- -- -- --" + i);
			if(i % 20 == 0) { Thread.yield(); }}}; new Thread(runnable,"Stack long").start();
    new Thread(runnable, "Small honey").start();
}
Copy the code

This example discards the CPU after 20 executions, and immediately obtains scheduling rights to continue execution after each concession.

When you run the above program, you can get two results.

Result 1: Stack length gave up CPU resources, and Honey succeeded in the upper position.

Stack length -----29 Stack length -----30 Honey -----26 Stack length -----31Copy the code

Result 2: The stack length frees up CPU resources, and the stack length continues to run.

Stack length -----28 Stack length -----29 Stack length -----30 Stack length -----31Copy the code

If we add thread priority to two threads, the output is different.

thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.MAX_PRIORITY);
Copy the code

Because to small honey added the highest priority, stack long added the lowest priority, even if the stack long start first, the small honey still has a great probability than the stack long will output, we can try.

Similarities and differences between yield and sleep

1) Both yield and sleep can suspend the current thread. Sleep can specify a specific sleep time, while yield depends on the CPU time slice.

2) Both yield and sleep will not release the lock resource if the lock is already held during the pause.

3) Yield cannot be interrupted, while sleep can accept interruption.

conclusion

Stack length is not used yield, it feels useless.

If I had to use it, it would be in one sentence: The yield method can be used to control multithreading well. For example, when performing a complex task, if you are worried about consuming too many resources, you can use the yield method to yield the scheduling right of the current CPU after completing an important task and continue the task until it is obtained next time. In this way, you can not only finish your important task, but also finish the task next time. It also gives other threads a chance to run, preventing one thread from hogging CPU resources for too long.

A latest Java technology tutorial, including Java foundation, multithreading, JVM, new features and more actual combat dry goods.