juc学习备忘录--交替打印

19 篇文章 0 订阅
18 篇文章 0 订阅
package com.wyz.juc;

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

/**
 * 交替打印的面试题:
 * 编写一个程序,开启三个线程,这三个线程的ID分别是A、B、C ,
 * 每个线程将自己的ID在屏幕上打印10遍,要求输出的结果必须按顺序显示。
 * 如:ABCABCABC....依次递归
 *
 * @author WangChong
 */
public class TestABCAlternate {
    public static void main(String[] args) {
        AlternateDemo alternateDemo = new AlternateDemo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    alternateDemo.loopA(i);
                }
            }
        },"A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    alternateDemo.loopB(i);
                }
            }
        },"B").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    alternateDemo.loopC(i);
                    System.out.println("------------");
                }
            }
        },"C").start();
    }
}


class AlternateDemo {
    private int number = 1;//当前正在执行线程的标记
    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();

    /**
     * 线程A循环
     * @param totalLoop 第几次循环
     */
    public void loopA(int totalLoop) {
        lock.lock();
        try {
            //1.判断
            if (number != 1) {
                condition1.await();
            }
            //2.打印
            for (int i = 0; i < 1; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i + totalLoop);
            }
            //3.唤醒 线程2
            number = 2;
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 线程B循环
     * @param totalLoop 第几次循环
     */
    public void loopB(int totalLoop) {
        lock.lock();
        try {
            //1.判断
            if (number != 2) {
                condition2.await();
            }
            //2.打印
            for (int i = 0; i < 1; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i + totalLoop);
            }
            //3.唤醒 线程3
            number = 3;
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 线程C循环
     * @param totalLoop 第几次循环
     */
    public void loopC(int totalLoop) {
        lock.lock();
        try {
            //1.判断
            if (number != 3) {
                condition3.await();
            }
            //2.打印
            for (int i = 0; i < 1; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i + totalLoop);
            }
            //3.唤醒 线程1
            number = 1;
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值