Java Multithreading: "Basic article" 06 of the thread concessions

Source: Internet
Author: User
Tags execution printf static class thread

1. Yield () Introduction

The role of yield () is to give in. It allows the current thread to move from "Run state" to "ready state", this allows other waiting threads with the same priority to get execution rights, but there is no guarantee that after the current thread calls yield (), other threads with the same priority will be able to execute; it may also be that the current thread has entered the Run status "continue running!"

2. Yield () example

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

Yieldtest.java source
class Threada extends thread{public
    Threada (String name) { 
        super (name); 
    } 
    Public synchronized void Run () {to 
        (int i=0 i <10; i++) { 
            System.out.printf ("%s [%d]:%d\n", This.getname (), This.getpriority (), i); 
            I divide 4 o'clock, call yield
            if (i%4 = 0)
                Thread.yield (); 
    
}}} public class yieldtest{public 
    static void Main (string[] args) { 
        Threada t1 = new Threada ("T1"); 
        Threada t2 = new Threada ("T2"); 
        T1.start (); 
        T2.start ();
    } 

(of one) run result:

T1 [5]:0
T2 [5]:0
T1 [5]:1
t1 [5]:2 t1
[5]:3 T1]
[5]:4
t1
[5]:5 t1] [5]:6 T1] [a] [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]:6 T2] [5]:7< c18/>t2 [5]:8
T2 [5]:9

Results show:

Thread T1 does not switch to thread T2 when it can be 4 integers. This indicates that yield () may allow the thread to enter the "Ready state" by "run State"; However, it does not necessarily give the other threads access to CPU execution (that is, other threads enter the "Run State"), even if this "other thread" has the same priority as the thread that is currently calling yield ().

3. Comparison of yield () and wait ()

As we know, the effect of Wait () is to allow the current thread to enter the standby (blocking) state while it is running, releasing the sync lock. The role of yield () is to give in, and it will leave the current thread out of the "running state." The difference between them is:

(a) Wait () is to allow a thread to go from "Run state" to "Waiting (blocking)" state, without yield () to allow the thread to enter from "Run state" to the ready state.

Wait () is a synchronous lock in which the thread releases the object it holds, and the yield () method does not release the lock.

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

Yieldlocktest.java Source public
class yieldlocktest{ 
    
    private static Object obj = new Object ();
    
    public static void Main (string[] args) { 
        Threada t1 = new Threada ("T1"); 
        Threada t2 = new Threada ("T2"); 
        T1.start (); 
        T2.start ();
    } 
    
    Static class Threada extends thread{public
        Threada (String name) { 
            super (name); 
        } 
        public void Run () { 
            //Get the synchronization lock
            synchronized (obj) for obj object (
                int i=0 i <10; i++) { 
                    System.out.printf ("%s [%d]:%d\n", This.getname (), this.getpriority (), i); 
                    I divide 4 o'clock, call yield
                    if (i%4 = 0)
                        Thread.yield ();
                } 
    } 
}}

(one time) run result:

T1 [5]:0
T1 [5]:1
T1 [5]:2
t1 [5]:3 t1
[5]:4 T1]
[5]:5
t1
[5]:6 t1] [5]:7 T1] [a] [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]:6 T2] [5]:7< c18/>t2 [5]:8
T2 [5]:9

Results show:

Two threads T1 and T2 are started in main thread main. T1 and T2 in run () refer to the synchronization lock for the same object, that is, synchronized (obj). While the T1 is running, it will call Thread.yield (), but T2 will not get CPU execution power. Because, T1 does not release "the synchronization lock which obj holds"!

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

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.