JUC-9.线程按序交替.md

1. 线程按序交替

1.1 题目

编写一个程序,开启3个线程,这三个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出的结果必须按顺序显示。
如;ABCABCABC。。依次打印

1.2 代码

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

/** * 编写一个程序,三个线程交替打印 * @author xiaobin * @date 2018/3/11 */
public class AbcAlternate {
    public static void main(String[] args) {
        final AtlernateDemo ad = new AtlernateDemo();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i  = 0 ; i <= 20 ; i++) {
                    ad.loopA(i);
                }
            }
        },"A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i  = 0 ; i <= 20 ; i++) {
                    ad.loopB(i);
                }
            }
        },"B").start();

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

    }
}

class AtlernateDemo {
    //当前正在执行线程的标记
    private int number = 1 ;

    private Lock lock = new ReentrantLock();
    private Condition condtion1 = lock.newCondition();
    private Condition condtion2 = lock.newCondition();
    private Condition condtion3 = lock.newCondition();


    /** * * @param totalLoop 循环几轮 */
    public void loopA(int totalLoop) {
        lock.lock();
        try {
            //1. 先判断
            if (number != 1) {
                condtion1.await();
            }
            //2. 打印
            for (int i = 1; i <= 5 ; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i
                        + "\t" + totalLoop);

            }

            //3. 唤醒
            number = 2 ;
            condtion2.signal();
        } catch (Exception e) {

        } finally {
            lock.unlock();
        }

    }

    /** * * @param totalLoop 循环几轮 */
    public void loopB(int totalLoop) {
        lock.lock();
        try {
            //1. 先判断
            if (number != 1) {
                condtion2.await();
            }
            //2. 打印
            for (int i = 1; i <= 15 ; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i
                        + "\t" + totalLoop);

            }

            //3. 唤醒
            number = 3 ;
            condtion3.signal();
        } catch (Exception e) {

        } finally {
            lock.unlock();
        }
    }

    /** * * @param totalLoop 循环几轮 */
    public void loopC(int totalLoop) {
        lock.lock();
        try {
            //1. 先判断
            if (number != 1) {
                condtion3.await();
            }
            //2. 打印
            for (int i = 1; i <= 20 ; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i
                        + "\t" + totalLoop);

            }

            //3. 唤醒
            number = 1 ;
            condtion1.signal();
        } catch (Exception e) {

        } finally {
            lock.unlock();
        }
    }
}
    原文作者:JUC
    原文地址: https://blog.csdn.net/xbliu564/article/details/79521902
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞