Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the concession of yield usage by Java Thread

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Java thread concession yield usage example analysis, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

A little bit of eye contact

The yield () method is somewhat similar to the sleep () method. It is a static method provided by the Thread class. It can also pause the currently executing thread, but it does not block the thread, it just puts the thread into a ready state. Yield simply pauses the current thread and reschedules the system's thread scheduler. It is entirely possible that when a thread calls the yield () method, the thread scheduler schedules it to execute again.

When a thread calls the yield method to pause, only threads in the ready state with the same priority as the current thread, or with a higher priority than the current thread, will get the opportunity to execute.

Two code

Public class YieldTest extends Thread {public YieldTest (String name) {super (name);} / defines the run method as the thread executor public void run () {for (int I = 0; I < 50; iSum +) {System.out.println (getName () + "" + I); / / when I equals 20:00, use the yield method to get the current thread to concede if (I = 20) {Thread.yield () } public static void main (String [] args) throws Exception {/ / start two concurrent threads YieldTest yt1 = new YieldTest ("Advanced"); / / set the ty1 thread to the highest priority / / yt1.setPriority (Thread.MAX_PRIORITY); yt1.start (); YieldTest yt2 = new YieldTest ("low"); / / set the yt2 thread to the lowest priority / / yt2.setPriority (Thread.MIN_PRIORITY); yt2.start ();}}

Three operation

. Low-level 18 low-level 19 high-level 15 low-level 20 high-level 16 high-level 17 high-level 19 high-level 19 high-level 20 low-level 22 low-level 23 low-level 24 low-level 25 low-level 26.

Four instructions

1 at this time, the high-level and low-level threads have the same priority.

After calling the yield, the 2 thread gives the execution opportunity to other threads with the same priority.

Five code 2

Public class YieldTest extends Thread {public YieldTest (String name) {super (name);} / defines the run method as the thread executor public void run () {for (int I = 0; I < 50; iSum +) {System.out.println (getName () + "" + I); / / when I equals 20:00, use the yield method to get the current thread to concede if (I = 20) {Thread.yield () } public static void main (String [] args) throws Exception {/ / start two concurrent threads YieldTest yt1 = new YieldTest ("Advanced"); / / set the ty1 thread to the highest priority yt1.setPriority (Thread.MAX_PRIORITY); yt1.start (); YieldTest yt2 = new YieldTest ("low"); / / set the yt2 thread to the lowest priority yt2.setPriority (Thread.MIN_PRIORITY); yt2.start ();}}

Six run 2

Senior 17, Senior 18, Senior 19, Senior 20, Senior 21, Senior 22, Senior 23, Senior 25, Senior 26, Senior 27, 28. Senior 48 Senior 49 low level 12 low level 13 low level 14 low level 15 low level 16 low level 17 low level 18 low level 19 low level 20 low level 21 low level 22 low level 23

Seven instructions 2

1 two threads have different priorities.

2 after a high-priority thread pauses by calling the yield method, the system does not have a thread with the same priority or higher priority, so the thread continues to execute.

This is the answer to the sample analysis question about Java thread concession yield usage. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report