“This is the 23rd day of my participation in the Gwen Challenge.

LockSupport:

What it is, what it does, where to download it, how to play it;Copy the code

Four steps

source

Nature of LockSupport:

This is the enhanced version of the thread wake-up mechanism -> the improved version has two special methods;

  • Park ();
  • UnPark ();

Wait () and notify();


For object, wait() makes the thread wait and notify () wakes it up.


The await () of condition in juc makes the thread wait and wakes it up using the signal() method;

Implementation of standards, producer and consumer models:

Synchronized 1. Use wait() and notify () — synchronized

package com.atguowang.thirdinterview.juc;



import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;



import java.util.concurrent.locks.LockSupport;



/ * *

 * @author lucas

 * @time 2020/10/27/10:29

* @description lock object, what is it, can do, from where, how to play

* AB learning method

* * /

public class TestLockSupport {

/ / lock object

    static Object data = new Object();



    public static void main(String[] args) {

/ / print

        //System.out.println(Thread.currentThread().getName()+"\t"+"Running thread");



        new Thread(() -> {

            synchronized (data) {

                System.out.println(Thread.currentThread().getName() + "\t" + "-- thread come in");

                try {

data.wait(); // Wait for the operation



                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

                System.out.println(Thread.currentThread().getName() + "\t" + "Awakened");

            }

        }, "A").start();





// The second thread wakes up;



        new Thread(() -> {

            synchronized (data) {

                data.notify();

                System.out.println(Thread.currentThread().getName() + "\t" + "Notice");

            }



        }, "B").start();

    }

}



Copy the code

Resolution:

When a thread is created and entered, the synchronized lock object is the same as that of the other thread, and the synchronized lock object can be awakened directly when the thread is entered. That is, multiple threads can wait and wake up a reentrant lock,

Exception 1: If synchronized is deleted and only wait and notify () is kept, does it still work?

Cannot enter wait;

Exception in thread “A” Exception in thread “B” java.lang.IllegalMonitorStateException

package com.atguowang.thirdinterview.juc;



import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;



import java.util.concurrent.locks.LockSupport;



/ * *

 * @author lucas

 * @time 2020/10/27/10:29

* @description lock object, what is it, can do, from where, how to play

* AB learning method

* * /

public class TestLockSupport {

/ / lock object

    static Object data = new Object();



    public static void main(String[] args) {

/ / print

        //System.out.println(Thread.currentThread().getName()+"\t"+"Running thread");



        new Thread(() -> {

//            synchronized (data) {

                System.out.println(Thread.currentThread().getName() + "\t" + "-- thread come in");

                try {

data.wait(); // Wait for the operation



                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

                System.out.println(Thread.currentThread().getName() + "\t" + "Awakened");

//            }

        }, "A").start();





// The second thread wakes up;



        new Thread(() -> {

//            synchronized (data) {

                data.notify();

                System.out.println(Thread.currentThread().getName() + "\t" + "Notice");

//            }



        }, "B").start();

    }

}



Copy the code

The second case is unusual:

What happens if we switch the wait->notify method to wake up and then wait

This can be used to let the waiting thread sleep for three seconds, the first to perform the wake up thread, try;


package com.atguowang.thirdinterview.juc;



import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;



import java.sql.Time;

import java.util.concurrent.TimeUnit;

import java.util.concurrent.locks.LockSupport;



/ * *

 * @author lucas

 * @time 2020/10/27/10:29

* @description lock object, what is it, can do, from where, how to play

* AB learning method

* * /

public class TestLockSupport {

/ / lock object

    static Object data = new Object();



    public static void main(String[] args) {





        new Thread(() -> {

// Pause for a few seconds

            try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); }

            synchronized (data) {

                System.out.println(Thread.currentThread().getName() + "\t" + "-- thread come in");

                try {

data.wait(); // Wait for the operation

                } catch (InterruptedException e) {

                    e.printStackTrace(); }

                System.out.println(Thread.currentThread().getName() + "\t" + "Awakened"); }

        }, "A").start();





// The second thread wakes up;



        new Thread(() -> {

            synchronized (data) {

                data.notify();

                System.out.println(Thread.currentThread().getName() + "\t" + "Notice");

            }



        }, "B").start();

    }

}



Copy the code

The iron triangle cannot be broken, or rather; Wait and notify cannot be changed within a thread.

package com.atguowang.thirdinterview.juc;



import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;



import java.sql.Time;

import java.util.concurrent.TimeUnit;

import java.util.concurrent.locks.LockSupport;



/ * *

 * @author lucas

 * @time 2020/10/27/10:29

* @description lock object, what is it, can do, from where, how to play

* AB learning method

* * /

public class TestLockSupport {

/ / lock object

    static Object data = new Object();



    public static void main(String[] args) {





        new Thread(() -> {

// Pause for a few seconds

            //try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); }

            synchronized (data) {

                System.out.println(Thread.currentThread().getName() + "\t" + "-- thread come in");

try { data.wait(); // Wait for the operation

                      System.out.println("In a state of waiting.");

                      data.notify();

                        System.out.println("In an awakened state.");

                } catch (InterruptedException e) {

                    e.printStackTrace(); }

                System.out.println(Thread.currentThread().getName() + "\t" + "Awakened"); }

        }, "A").start();





// The second thread wakes up;



        new Thread(() -> {

            synchronized (data) {

                data.notify();

                System.out.println(Thread.currentThread().getName() + "\t" + "Notice");

            }



        }, "B").start();

    }

}



Copy the code

We also have to make sure that the specific data, we convert the thread into the specific use, the method is a simple process, if we say an object we can get;


Already is a Java. Util. Concurrent. The Locks. Already comparative data, then we will use the process directly into specific data; This extension to reentrant mutex, synchronized,


Because he has a specific condition of a lock state —


ReentrantLock —- represents await () and specific signal();

The use of await () and so on takes advantage of the fact that the state (condition) is an interface. There are default methods and specific wake up — wait to block;

Lock ()– >sync.lock()– AQS

AbstractQueuedSynchronizer – “AQS AbstractQueuedSychronzier view LockSupport display results – can be a specific data display;

JUC–>AQS–>LockSupport

Luca message

At present, the bottom AQS of reentrant lock is the lock support at the bottom level. Therefore, for locks, after understanding the classification, it is good to keep the knowledge at the bottom level unchanged.

Good night. I’m Luka. Give it a thumbs up