天天看点

使用newCondition方法+ReentranLock实现本地队列

 直接给一个demo吧

package com.dxd.demo.service;

import com.dxd.demo.pojo.Eat;
import com.dxd.demo.pojo.GetUp;
import com.dxd.demo.pojo.Sleep;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class testService {
    private static final Lock lock = new ReentrantLock();

    Condition getUpCondition = lock.newCondition();
    Condition eatCondition = lock.newCondition();
    Condition sleepCondition = lock.newCondition();

    //起床
    GetUp getUp = new GetUp();
    //吃饭
    Eat eat;
    //睡觉
    Sleep sleep;

    public static void main(String[] args) {
        testService testService = new testService();
        int getUpCount = 0;
        int eatCount = 0;
        int sleepCount = 0;

        while (getUpCount++ < 10)
            //起床
            getUpTest(testService);

        while (sleepCount++ < 10)
            //睡觉
            sleepTest(testService);

        while (eatCount++ < 10)
            //吃饭
            eatTest(testService);

    }

    //起床
    private static void getUpTest(testService testService) {
        new Thread(() -> {
            try {
                lock.lock();
                while (testService.getUp == null) {
                    try {
                        //加入条件队列
                        testService.getUpCondition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                //不让起床
                testService.setGetUp(null);
                //不让睡觉
                testService.setSleep(null);

                //准备吃饭
                testService.setEat(new Eat());

                //唤醒吃饭线程,开始吃饭
                testService.eatCondition.signal();
            } finally {
                lock.unlock();
            }
        }, "getUp").start();
    }

    //吃饭
    private static void eatTest(testService testService) {
        new Thread(() -> {
            try {
                lock.lock();
                while (testService.eat == null) {
                    try {
                        //加入条件队列
                        testService.eatCondition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                //不让起床
                testService.setGetUp(null);
                //不让吃饭
                testService.setEat(null);

                //准备睡觉
                testService.setSleep(new Sleep());

                //唤醒睡觉线程,开始睡觉
                testService.sleepCondition.signal();
            } finally {
                lock.unlock();
            }
        }, "eat").start();
    }

    //睡觉
    private static void sleepTest(testService testService) {
        new Thread(() -> {
            try {
                lock.lock();
                while (testService.sleep == null) {
                    try {
                        //加入条件队列
                        testService.sleepCondition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                //不让睡觉
                testService.setSleep(null);
                //不让吃饭
                testService.setEat(null);

                //准备起床
                testService.setGetUp(new GetUp());
                //唤醒起床线程,准备起床
                testService.getUpCondition.signal();

            } finally {
                lock.unlock();
            }
        }, "sleep").start();
    }


    public void setGetUp(GetUp getUp) {
        this.getUp = getUp;
    }

    public void setEat(Eat eat) {
        this.eat = eat;
    }

    public void setSleep(Sleep sleep) {
        this.sleep = sleep;
    }
}
           

实现condition队列,可以自定义线程的执行顺序。