Java Multithreading Series--"basic article" 06 of the thread concessions

Source: Internet
Author: User

Overview

In this chapter, the thread concession method yield () in thread is introduced. The topics covered include:
1. Yield () Introduction
2. Yield () example
3. Yield () comparison to wait ()

Reprint Please specify source :http://www.cnblogs.com/skywang12345/p/3479243.html

1. Yield () Introduction

Yield () is the function of concession. It allows the current thread to enter the "Ready state" from the "running state", allowing other waiting threads with the same priority to get execution, but there is no guarantee that other threads with the same priority will be able to gain execution after the current thread calls yield (), or that the current thread is entering the " Run status "continue running!

2. Yield () example

Below, use the example to see how it is used.

1//Source Code of Yieldtest.java2Class ThreadaExtendsthread{3PublicThreada (String name) {4Super(name);5}6PublicSynchronizedvoidRun () {7Forint i=0; I <10; i++){8 System.out.printf ("%s [%d]:%d\n",This.getname (),This. getpriority (), i);9//I divide 4 o'clock, call yield10if (i%4 = = 0)11Thread.yield ();12}13}14}1516 public class yieldtest{17 public void main (string[] args) {18 Threada t1 = new Threada ("T1" ); 19 Threada t2 = new Threada ("T2" ); 20  T1.start (); 21  T2.start ();   23}       

(one time) The result of the operation :

T1 [5]:0T2 [5]:0T1 [5]:1T1 [5]:2T1 [5]:3 t1 [5]:4 T1 [5]:5 T1 [5]:6 T1 [5]:7 T1 [5]:8 T1 [5]:9 T2 [5]:1 T2 [5]:2 T2 [5]:3 T2 [5]:4 T2 [5]:5 T2 [5]:6T2 [5]:7 T2 [5]:8 T2 [5]:9                  

Result Description :
"Thread T1" does not switch to "thread T2" when it can be 4 integers. This indicates that yield () can allow a thread to go from "Running state" to "ready state", but it does not necessarily allow other threads to get CPU execution (that is, other threads go into "running state"), even if this "other thread" has the same priority as the thread that is currently calling yield ().

3. Yield () comparison to wait ()

We know that the role of Wait () is to allow the current thread to enter the "waiting (blocking) state" from the "Running state" and also release the synchronization lock. Yield () is a concession, and it also leaves the current thread out of the "running state". The difference between them is:
Wait () is a thread that enters the "waiting (blocking) state" from the "running state" and does not yield () to allow the thread to go from "Running state" to "ready state".
Wait () is the thread that releases the synchronization lock on the object it holds, and the yield () method does not release the lock.

The following example shows that yield () does not release the lock.

1//Source Code of Yieldlocktest.java2PublicClassyieldlocktest{34Privatestatic Object obj =NewObject ();56PublicStaticvoidMain (string[] args) {7 Threada t1 =New Threada ("T1");8 Threada t2 =New Threada ("T2");9T1.start ();10T2.start ();11}1213StaticClass ThreadaExtendsthread{14PublicThreada (String name) {15Super(name);16}17PublicvoidRun () {18//Gets the synchronization lock for the Obj object19Synchronized(obj) {20Forint i=0; I <10; i++21 System.out.printf ("%s [%d]:%d\n", this.getname (), this.getpriority (), i); 22 // I divide 4 o'clock, call Yield23 if (i%4 = = 024  Thread.yield (); 25 }26 }27  28 } 29}            

Run results (one time) :

T1 [5]:0T1 [5]:1T1 [5]:2T1 [5]:3T1 [5]:4 t1 [5]:5 T1 [5]:6 T1 [5]:7 T1 [5]:8 T1 [5]:9 T2 [5]:0 T2 [5]:1 T2 [5]:2 T2 [5]:3 T2 [5]:4 T2 [5]:5 T2 [5]:6T2 [5]:7 T2 [5]:8 T2 [5]:9                  

Result Description :
The main thread, main thread, starts with two threads T1 and t2. T1 and T2 in run () refer to the synchronization lock of the same object, that is, synchronized (obj). While the T1 is running, it will call Thread.yield (), but T2 will not get CPU execution. Because, T1 did not release "the synchronization lock held by obj"!

Java Multithreading Series--"basic article" 06 of the thread concessions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.