Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the differences between sleep () and yield () in java

Shulou Source: shulou.com Published: 2022-06-02 02:39:32 10月23日 Update

This article mainly introduces what are the differences between sleep () and yield () in java, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.

2.4 the difference between sleep () and yield ()

1) sleep () brings the current thread into a stagnant state, so the thread executing sleep () will definitely not execute within a specified period of time; yield () just brings the current thread back to the executable state, so the thread executing yield () may be executed again as soon as it enters the executable state.

2) sleep () can give low-priority threads the opportunity to execute, of course, it can also give the same priority and high-priority threads the opportunity to execute; yield () can only make the same priority threads have the opportunity to execute.

Example 15:

Class TestThreadMethod extends Thread {

Public static int shareVar = 0

Public TestThreadMethod (String name) {

Super (name)

}

Public void run () {

For (int item0; I

Tags: Thread code function object state flag result priority run opportunity comment article form different data time synchronization three two representative Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MariaDB MySQL Shulou Tech Info macOS