Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between sleep () and yield () in Java programming language

Shulou Source: shulou.com Published: 2022-06-03 19:16:18 10月01日 Update

This article mainly explains "what is the difference between sleep () and yield () in Java programming language". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the difference between sleep () and yield () in Java programming language".

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.

Class TestThreadMethod extends Thread {public static int shareVar = 0; public TestThreadMethod (String name) {super (name);} public void run () {for (int iTuno; I)

Tags: Threads priorities opportunities programming languages languages programming states results learning code content comments differences just just ideas situations articles time more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia Linux Redmi Docker