多线程交替打印,循环输出ABBCCC

多线程交替打印,循环输出ABBCCC

  1. 使用synchronized/wait/notifyAll
/**
 * 按照ABBCCC...的顺序打印20次
 */
public class ABBCCC_Wait {

    public static void main(String[] args) {
        ABBCCCPrinter printer = new ABBCCCPrinter(1, 5);
        new Thread(() -> {
            printer.print("A", 1, 2);
        }, "t1").start();

        new Thread(() -> {
            printer.print("B", 2, 3);
        }, "t2").start();

        new Thread(() -> {
            printer.print("C", 3, 1);
        }, "t3").start();
    }
}

/**
 * 打印类
 */
class ABBCCCPrinter{
    /**
     * 公共标记位
     */
    private int flag;

    /**
     * 循环次数
     */
    private int loopNumber;

    public ABBCCCPrinter(int flag, int loopNumber) {
        this.flag = flag;
        this.loopNumber = loopNumber;
    }

    /**
     * 打印方法
     * @param str : 打印字符串
     * @param waitFlag : 等待标记位
     * @param nextFlag : 下一个标记位
     */
    public void print(String str, int waitFlag, int nextFlag){
        for (int i = 0; i < loopNumber; i++) {
            synchronized (this){
                // while循环防止虚假唤醒
                while (flag != waitFlag){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                for (int j = 0; j < waitFlag; j++) {
                    System.out.print(str);
                }
                flag = nextFlag;
                this.notifyAll();

            }
        }
    }
}
  1. 使用lock/await/signalAll
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ABBCCC_Await {

    public static void main(String[] args) {
        final Lock lock = new ReentrantLock();
        final Condition conditionA = lock.newCondition();
        final Condition conditionB = lock.newCondition();
        final Condition conditionC = lock.newCondition();
        ABBCCC_Await_Printer printer = new ABBCCC_Await_Printer(1, 5);
        new Thread(() -> {
            printer.print("A", 1, 2, lock, conditionA, conditionB);
        }, "t1").start();

        new Thread(() -> {
            printer.print("B", 2, 3, lock, conditionB, conditionC);
        }, "t2").start();

        new Thread(() -> {
            printer.print("C", 3, 1, lock, conditionC, conditionA);
        }, "t3").start();
    }
}


class ABBCCC_Await_Printer {

    /**
     * 公共标记
     */
    private int flag;

    /**
     * 循环次数
     */
    private int loopNumber;

    public ABBCCC_Await_Printer(int flag, int loopNumber) {
        this.flag = flag;
        this.loopNumber = loopNumber;
    }


    /**
     * @param str : 打印字符串
     * @param waitFlag : 等待标记位
     * @param nextFlag : 下一个标记位
     * @param waitCondition :等待条件
     * @param signalCondition  :唤醒
     */
    public void print(String str, int waitFlag, int nextFlag,
                      Lock lock,
                      Condition waitCondition,
                      Condition signalCondition) {
        for (int i = 0; i < loopNumber; i++) {
            lock.lock();
            try {
                // while循环防止虚假唤醒
                while (flag != waitFlag){
                    try {
                        waitCondition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                for (int j = 0; j < waitFlag; j++) {
                    System.out.print(str);
                }
                flag = nextFlag;
                signalCondition.signalAll();
            }finally {
                lock.unlock();
            }

        }



    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值