使用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队列,可以自定义线程的执行顺序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值