题目:多线程实现交替输出 A B C ,连续输出20遍

解法一

package com.qianfeng.yxl.the19.example;

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

/**
 * lenovo  2019/8/11  13:54
 */
public class ABC {
    private static final int COUNT = 20 * 3;//20是每个字母输出20遍
    private static int state = 1;          // 3是有3个字母交替输出,COUNT是总共输出的遍数
    public static void main(String[] args) {
        final Lock l = new ReentrantLock();

        Thread A = new Thread(new Runnable(){
            @Override
            public void run() {
                while (state <= COUNT) {
                    l.lock();
                    if(state <= COUNT && state%3==1){
                        System.out.println(Thread.currentThread().getName() + "线程->" + state + "A");
                        state ++;
                    }
                    l.unlock();
                }
            }
        },"A");
        Thread B = new Thread(new Runnable(){
            @Override
            public void run() {
                while (state <= COUNT) {
                    l.lock();
                    if(state <= COUNT && state%3==2){
                        System.out.println(Thread.currentThread().getName() + "线程->" + state + "B");
                        state ++;
                    }
                    l.unlock();
                }
            }
        },"B");
        Thread C = new Thread(new Runnable(){
            @Override
            public void run() {
                while (state <= COUNT) {
                    l.lock();
                    if(state <= COUNT && state%3==0){
                        System.out.println(Thread.currentThread().getName() + "线程->" + state + "C");
                        state++;
                    }
                    l.unlock();
                }
            }
        },"C");
        A.start();
        B.start();
        C.start();
    }
}

解法二

/**
 * lenovo  2019/8/14  15:19
 */
public class AlternativePrint {
    private Lock lock = new ReentrantLock();
    Condition conditionA = lock.newCondition();
    Condition conditionB = lock.newCondition();
    Condition conditionC = lock.newCondition();
    private int num = 1;
    private int count = 1;
    public void printA() throws InterruptedException {
        lock.lock();
        try {
            if(num != 1){
                conditionA.await();
            }
            System.out.println("A");
            num = 2;
            conditionB.signal();
        }finally {
            lock.unlock();
        }
    }
    public void printB() throws InterruptedException {
        lock.lock();
        try {
            if(num != 2){
                conditionB.await();
            }
            System.out.println("B");
            num = 3;
            conditionC.signal();
        }finally {
            lock.unlock();
        }
    }
    public void printC() throws InterruptedException {
        lock.lock();
        try {
            if(num != 3){
                conditionC.await();
            }
            System.out.println("C");
            System.out.println("------"+count+"------");
            count++;
            num = 1;
            conditionA.signal();
        }finally {
            lock.unlock();
        }
    }
}
/**
 * lenovo  2019/8/14  15:24
 */
public class Test {
    public static void main(String[] args) {
        AlternativePrint alternative = new AlternativePrint();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0;i < 20;i++){
                    try {
                        alternative.printA();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0;i < 20;i++){
                    try {
                        alternative.printB();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0;i < 20;i++){
                    try {
                        alternative.printC();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值