Condition

Wait /notify/notifyAll

Condition has a similar function, but may have more features such as: no response to interrupt, can specify the end of the wait, can have multiple conditions (new multiple conditions).

Condition await is similar to wait until the lock is acquired

A, use,

/ * * *@authorDay and night of wood */
public class ConditionTest {
    public static Lock lock = new ReentrantLock();
    //
    public static Condition cd01 = lock.newCondition();
    public static void main(String[] args) throws InterruptedException {
        ConditionTest test = new ConditionTest();
        new Thread(test::say01).start();
        Thread.sleep(5000);
        new Thread(test::say02).start();
    }

    /** * says: I'm hungry */
    public void say01(a){
        try{
            lock.lock();
            System.out.println("I'm hungry.");
            cd01.await();
            System.out.println("Full");
        } catch (Exception e){
            e.printStackTrace();
        } finally{ lock.unlock(); }}/** * says: Dinner is ready */
    public void say02(a){
        try{
            lock.lock();
            cd01.signal();
            System.out.println("Dinner is served.");
        } catch (Exception e){
            e.printStackTrace();
        } finally{ lock.unlock(); }}} I am hungry and fullCopy the code

Two, do not respond to interrupt

Look at say01 say02 interruptible Say03 uninterruptible

public class ConditionTest02 {

    public static Object obj = new Object();
    public static Lock lock = new ReentrantLock();
    public static Condition cd01 = lock.newCondition();
    public static void main(String[] args) throws InterruptedException {
        ConditionTest02 test = new ConditionTest02();
        Thread thread = new Thread(test::say01);
        Thread thread02 = new Thread(test::say02);
        Thread thread03 = new Thread(test::say03);


        thread.start();
        thread02.start();
        thread03.start();

        Starve them for 5 seconds
        Thread.sleep(5000);

        / / the interrupt
        thread.interrupt();
        thread02.interrupt();
        Say03 will interrupt and fail
        thread03.interrupt();

    }

    /** * says: Jack is hungry */
    public void say01(a){
        try{
            lock.lock();
            System.out.println("Jack is hungry.");
            System.out.println("Cockroaches waiting to feed.");
            cd01.await();
            System.out.println("Jack Bauer is full.");
        }catch (InterruptedException e){
            System.out.println("Cockroaches are out looking for food.");
        }catch (Exception e){
            e.printStackTrace();
        } finally{ lock.unlock(); }}/** * says: Xiao Ming is hungry */
    public void say02(a){
        try{
            synchronized (obj){
                System.out.println("Xiao Ming is hungry.");
                System.out.println("Xiao Ming waits for feeding.");
                obj.wait();
                System.out.println("Xiao Ming is full."); }}catch (InterruptedException e){
            System.out.println("Xiao Ming is out looking for food."); }}/** * says: moonbird hungry interrupt useless */
    public void say03(a){
        try{
            lock.lock();
            System.out.println("Moonbird is hungry.");
            System.out.println("Moonbirds waiting to feed.");
            cd01.awaitUninterruptibly();
            System.out.println("Moonbird is full.");
        }catch (Exception e){
            System.out.println("Moonbirds are out looking for food.");
            e.printStackTrace();
        } finally{ lock.unlock(); }}} Output result: Ming is hungry, Ming is waiting for feeding, And Xiaoqiang is hungry. Xiaoqiang is hungry, and Xiaoqiang is waiting for feedingCopy the code

Wait a minute

Condition can await await for a while until he realizes that no one is listening to him and then stops awaiting await

An Object wait can also specify a timeout

public class ConditionTest03 {
    public static Lock lock = new ReentrantLock();
    //
    public static Condition cd01 = lock.newCondition();
    public static void main(String[] args)   {
        ConditionTest03 test = new ConditionTest03();
        new Thread(test::say01).start();


    }

    /** * says: I'm hungry */
    public void say01(a){
        try{
            lock.lock();
            System.out.println("I'm hungry.");
            System.out.println("Waiting to be fed");
            //

            // The unit needed for this is millisecond *1000*1000000 which is 5000000000L millisecond = 5 seconds
            // Nanos is equal to 5000000000L
            long nanos = TimeUnit.SECONDS.toNanos(5);

            long res = cd01.awaitNanos(5000000000L);
            // It can also return true false
            // boolean res = cd01.await(10,TimeUnit.SECONDS);
            / / timeout
            if (res <= 0){
                System.out.println("No one will feed me.");
            } else {
                System.out.println("I've been fed enough."); }}catch (Exception e){
            e.printStackTrace();
        } finally{ lock.unlock(); }}}Copy the code

4. Designate time

A wait can be specified to end at a specified point in time

public class LockTest04 {
    / / a new lock
    public static Lock lock = new ReentrantLock(false);
    public static Condition cd = lock.newCondition();

    public static void main(String[] args) throws InterruptedException {
        new Thread(()->{
            try {
                lock.lock();
                long l = System.currentTimeMillis();
                l = l +1000*10;
                Date date = new Date(l);
                boolean  res = cd.awaitUntil(date);
                if(res) {
                    System.out.println("Be informed");
                } else {
                    System.out.println("I left without notice."); }}catch (Exception e){
                e.printStackTrace();
            }finally{ lock.unlock(); } }).start(); }}Copy the code

Create multiple conditions

Here is an example: Xiaoqiang and Xiaoyueyue two people playing toys xiaoqiang play not to play to Xiaoyueyue xiaoyueyue play not to play to Xiaoqiang

public class PlayTest {
    public static Lock lock = new ReentrantLock();
    public static Condition cd01 = lock.newCondition();
    public static void main(String[] args) {
        PlayTest test = new PlayTest();
        new Thread(test::xiaoqiangPlay).start();
        new Thread(test::xiaoxuexuePlay).start();
    }

    // Jack Bauer
    public void xiaoqiangPlay(a){
        try {
            while (true){
                lock.lock();
                System.out.println(Jack Bauer);
                Thread.sleep(5000);
                // Tell someone to play
                cd01.signal();
                // Wait to be informedcd01.await(); }}catch (Exception e) {
            e.printStackTrace();
        } finally{ lock.unlock(); }}// Play next month
    public void xiaoxuexuePlay(a){
        try {
            while(true){
                lock.lock();
                System.out.println("Little Yueyue play");
                Thread.sleep(5000);
                // Tell someone to play
                cd01.signal();
                // Wait to be informedcd01.await(); }}catch (Exception e) {
            e.printStackTrace();
        } finally{ lock.unlock(); }}}Copy the code

You can use two conditions

public class PlayTest02 {
    public static Lock lock = new ReentrantLock();
    public static Condition cd01 = lock.newCondition();
    public static Condition cd02 = lock.newCondition();
    public static void main(String[] args) {
        PlayTest02 test = new PlayTest02();
        new Thread(test::xiaoqiangPlay).start();
        new Thread(test::xiaoxuexuePlay).start();
    }

    // Jack Bauer
    public void xiaoqiangPlay(a){
        try {
            while (true){
                lock.lock();
                System.out.println(Jack Bauer);
                Thread.sleep(5000);
                // Tell someone to play
                cd01.signal();
                // Wait to be informedcd02.await(); }}catch (Exception e) {
            e.printStackTrace();
        } finally{ lock.unlock(); }}// Play next month
    public void xiaoxuexuePlay(a){
        try {
            while(true){
                lock.lock();
                System.out.println("Little Yueyue play");
                Thread.sleep(5000);
                // Tell someone to play
                cd02.signal();
                // Wait to be informedcd01.await(); }}catch (Exception e) {
            e.printStackTrace();
        } finally{ lock.unlock(); }}}Copy the code

Two conditions do not interfere with each other, and you can specify condition await/signal

Condition signalAll and notifyAll are similar and are not demonstrated in code

Six, summarized

  1. Create Condition (multiple conditions can be created without affecting each other)
  2. Can only be used after lock has acquired the lock
  3. Await supports interrupt without corresponding, timeout (Object wait also supports), end at a specified point in time
  4. Signal wakes up one thread. SignalAll wakes up all threads

Welcome to our official account: