当前位置:网站首页>Three queues CXQ, entrylist, waitset personal understanding analysis

Three queues CXQ, entrylist, waitset personal understanding analysis

2022-06-09 05:41:00 Cat cat God

Catalog

1.synchronized,wait(),notify() Methods, and so on ?

2.synchronized When relocking , How about the lock snatching process ?

3.wait() Method will cause the current thread to go to waitset In line ,waitset It is a circular two-way linked list

4.notify() The method is actually moving waitset The threads in either go to cxq Either entrylist in , The wake-up mechanism will be triggered when the synchronization method ends , according to Qmode Different types wake up with different rules .

5. General figure ( Personal painting )

6. Write code to verify

Personal summary : You can see that they are all by default jdk, To wake up a thread is to wake it up first waitset Thread in Wake up again cxq Medium


1.synchronized,wait(),notify() Methods, and so on ?

In fact, it is manipulation cxq,entrylist,waitset These three queues are realized . So it's necessary to sort it out

entrylist yes Double linked list

2.synchronized When relocking , How about the lock snatching process ?

As we all know, a heavyweight lock means that each thread passes through cas To modify ObjectMonitor in ower Field , The successful modification means that the lock has been snatched , If you fail, you will cas Join in cxq in ,cxq It's a stack structure .

3.wait() Method will cause the current thread to go to waitset In line ,waitset yes Circular double linked list

4.notify() The method is actually moving waitset The threads in either go to cxq Either entrylist in , The wake-up mechanism will be triggered when the synchronization method ends , according to Qmode Different types wake up with different rules .

5. General figure ( Personal painting )

 notif() according to Policy Do different things

Policy==0 : Put in entrylist The head of the queue
Policy==1 : Put in entrylist At the end of the queue
Policy==2 : Judge entrylist Is it empty , If it is empty, put entrylist in , Otherwise in the cxq Queue head position ( Default
Strategy )

Policy==3 : Judge cxq Is it empty , If it is empty , Put it directly into the head , Otherwise in the cxq At the end of the queue
 

When the synchronized code block is executed , What does wakeup look like ? according to Qmode Make a selection

according to QMode Policy wakes up :
QMode=2, take cxq The head node wakes up directly
QMode=3, If cxq Non empty , hold cxq The queue is placed in entrylist Tail of ( Sequential heel cxq Agreement )
QMode=4, If cxq Non empty , hold cxq The queue is placed in entrylist The head of ( Sequential heel cxq contrary )
QMode=0, Do nothing , Keep going down (QMode The default is 0) The default is 0
Qmode=0 The logic of judgment is to judge first entrylist Is it empty , If it's not empty , Then take out the first wake-up , Such as
If the result is empty, then start from cxq Get the first wakeup inside

6. Write code to verify

package com.imooc.controller.jdk;

import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: yk
 * @time: 2022/5/19 15:11
 */
public class synchonizedTest {
    private static final Object object=new Object();
    public static void main(String[] args) throws InterruptedException {
            synchonizedTest synchonizedTest=new synchonizedTest();
                synchonizedTest.methoda();
    }
    private void  methoda() throws InterruptedException {
        new Thread(()->{
            synchronized (object){
                System.out.println(Thread.currentThread().getName()+"start");
                try {
                    methodb();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"end");
            }
        },"A").start();
    }
    private void  methodb() throws InterruptedException {
        new Thread(()->{
            synchronized (object) {
                System.out.println(Thread.currentThread().getName()+"start");
                methodc();
                try {
                    TimeUnit.MICROSECONDS.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                object.notify();
                System.out.println(Thread.currentThread().getName()+"end");
            }
        },"B").start();
    }
    private void  methodc(){
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"start");
            synchronized (object) {
                System.out.println(Thread.currentThread().getName()+"end");
            }
        },"C").start();
    }

}

 A end Forever Cend front , in other words A Forever than C Get the lock first

a start-up b after wait,b start-up c after ,b Sleep 200ms( here c We have reached the entrance of the lock ), then b Wake up the a
here a,c Grab the lock together
result :a Forever c Grab the lock first

Personal summary : You can see that they are all by default jdk, To wake up a thread is to wake it up first waitset Thread in Wake up again cxq Medium

原网站

版权声明
本文为[Cat cat God]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090529559353.html