JUC并发编程九 并发架构--循环打印

使用wait-notify方式实现循环打印

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.TestCycle")
public class TestCycle {

    public static void main(String[] args) {
        WaitNotify waitNotify = new WaitNotify(1,5);

        new Thread(()->{
            waitNotify.print("a",1,2);
        }).start();

        new Thread(()->{
            waitNotify.print("b",2,3);
        }).start();

        new Thread(()->{
            waitNotify.print("c",3,1);
        }).start();
    }

}

class WaitNotify{

    private int flag; // 初始的状态值,使用整数作为标记
    private int loopNum;

    public WaitNotify(int flag, int loopNum){
        this.flag = flag;
        this.loopNum = loopNum;
    }

    public void print(String str, int flag, int nextFlag){
        for (int i = 0; i < loopNum; i++) {
            synchronized (this){
                while(this.flag != flag){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                System.out.print(str);
                this.flag = nextFlag;
                this.notifyAll();
            }
        }
    }
}

使用await-signal方式打印

import lombok.extern.slf4j.Slf4j;

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

@Slf4j(topic = "c.TestCycle")
public class TestCycle {

    static AwaitSignal awaitSignal = new AwaitSignal(5);
    public static void main(String[] args) {
        Condition a = awaitSignal.newCondition();
        Condition b = awaitSignal.newCondition();
        Condition c = awaitSignal.newCondition();

        new Thread(()->{
            awaitSignal.print("a",a,b);
        }).start();
        new Thread(()->{
            awaitSignal.print("b",b,c);
        }).start();
        new Thread(()->{
            awaitSignal.print("c",c,a);
        }).start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        awaitSignal.lock();
        try{
           a.signal();
        }finally {
            awaitSignal.unlock();
        }
    }
}

class AwaitSignal extends ReentrantLock {
    private int loopNum;

    public AwaitSignal(int loopNum){
        this.loopNum = loopNum;
    }

    public void print(String str, Condition current, Condition nextCondition){
        for (int i = 0; i < loopNum; i++) {
            lock();
            try{
                current.await();
                System.out.print(str);
                nextCondition.signal();
            }catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                unlock();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值