多线程使用AQS实现红绿灯依次亮

多线程实现红绿灯
首先是我们抽象的灯类:定义3个方法分别对应3种颜色
package HXZY.moth5.day5_11.linghtpract;

/**
 * およそ神
 * 抽象的灯类
 */
public abstract class Linght {
    abstract void gree() throws InterruptedException;
    abstract void red() throws InterruptedException;
    abstract void yellow() throws InterruptedException;
}

接下来分别是3种对应颜色的灯实现Runnable接口:

package HXZY.moth5.day5_11.linghtpract;

/**
 * およそ神
 */
public class Green implements Runnable {
    private Linght linght;

    public Green(Linght linght) {
        this.linght = linght;
    }

    @Override
    public void run() {
        while (true) {
            try {
                linght.gree();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

package HXZY.moth5.day5_11.linghtpract;

/**
 * およそ神
 */
public class Red implements Runnable {
    private Linght linght;

    public Red(Linght linght) {
        this.linght = linght;
    }

    @Override
    public void run() {
        while (true) {
            try {
                linght.red();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

package HXZY.moth5.day5_11.linghtpract;

/**
 * およそ神
 */
public class Yellow implements Runnable {
    private Linght linght;

    public Yellow(Linght linght) {
        this.linght = linght;
    }

    @Override
    public void run() {
        while (true) {
            try {
                linght.yellow();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

接下来是我们的控制台管理类:

package HXZY.moth5.day5_11.linghtpract;

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

/**
 * およそ神
 */
public class Aqs extends Linght {
    private final ReentrantLock lock = new ReentrantLock();
    private final Condition notRed = lock.newCondition();
    private final Condition notGreen = lock.newCondition();
    private final Condition notYellow = lock.newCondition();
    private int swich = 1;
    /**
     *调用绿灯的方法,绿灯亮,通知黄灯
     *调用红灯的方法,红灯亮,通知绿灯
     * 调用黄灯的方法,黄灯亮,通知红灯
     */
    @Override
    void gree() throws InterruptedException {
        try {
            lock.lock();//需要同步的代码首先加锁
            while (swich!=1)notGreen.await();//不是绿色阻塞
            for (int i = 0; i < 5; i++) {
                System.out.println("绿灯亮" + (i + 1) + "次");
                Thread.sleep(500);
            }
            this.swich = 2;
            this.notYellow.signal();//给绿灯发信息
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();//用完一定要解锁
        }
    }



    @Override
    void red() throws InterruptedException {
        try {
            lock.lock();//需要同步的代码首先加锁
            while (swich!=2)notRed.await();//不是绿色阻塞
            for (int i = 0; i < 5; i++) {
                System.out.println("红灯亮" + (i + 1) + "次");
                Thread.sleep(500);
            }
            this.swich = 3;
            this.notGreen.signal();//给黄灯发信息
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    @Override
    void yellow() throws InterruptedException {
        try {
            lock.lock();//需要同步的代码首先加锁
            while (swich!=3)notYellow.await();
            for (int i = 0; i < 5; i++) {
                System.out.println("黄灯亮" + (i + 1) + "次");
                Thread.sleep(500);
            }
            this.swich = 1;
            this.notRed.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

最后是我们的测试类:

package HXZY.moth5.day5_11.linghtpract;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * およそ神
 */
public class Test {
    public static void main(String[] args) {
        Aqs aqs = new Aqs();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.submit(new Yellow(aqs));
        executorService.submit(new Green(aqs));
        executorService.submit(new Red(aqs));
        executorService.shutdown();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值