JUC之分别打印A,B,C

JUC之分别打印A,B,C

JUC之分别打印A,B,C

线程的并发编程:

什么是juc: juc是java.util.concurrent下的类包,专门用于多线程的处理。

并发编程的本质目的就是为了充分发挥出CPU的计算能力。

上代码分析:

要求:一个程序,开启3个线程,线程id分别为A, B, C, 使这3个线程按顺序执行,运行次数自定。

/**
 * 要求:一个程序,开启3个线程,线程id分别为A, B, C, 使这3个线程按顺序执行,运行次数自定。
 */
public class TestABCAlternate {
    public static void main(String[] args) {
        ConcurrentDemo ad = new ConcurrentDemo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    ad.loopA(i);
                }
            }
        }, "A").start();

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

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    ad.loopC(i);

                    System.out.println("-----------------------");
                }
            }
        }, "C").start();
    }
}

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

    /*
    * 实例化ReentrantLock对象(可重入,可中断,公平锁)等特点
    * 可重入:能够对共享资源能够重复加锁,即当前线程获取该锁再次获取不会被阻塞
    * 可中断:当等待中,线程可以中断等待去执行其他任务
    * 公平锁:比较等待时间
    * */
    private Lock lock = new ReentrantLock();
    //产生与当前重入锁绑定的Condtion实例
    private Condition cd1 = lock.newCondition();
    private Condition cd2 = lock.newCondition();
    private Condition cd3 = lock.newCondition();

    public void loopA(int runTimes) {
        //上锁
        lock.lock();
        try {
            //1.判断
            if (flag != 1) {
                //进入等待状态
                cd1.await();
            }
            //打印
            for (int i = 0; i < 1; i++) {
                System.out.println(Thread.currentThread().getName() + "	" + i + "	" + runTimes);

            }
            //唤醒线程
            flag = 2;
            cd2.signal();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            //要在finally里面解锁,要不然可能会出现异常
            lock.unlock();
        }

    }

    public void loopB(int runTimes) {
        lock.lock();
        try {
            //1.判断
            if (flag != 2) {
                cd2.await();
            }
            //打印
            for (int i = 0; i < 1; i++) {
                System.out.println(Thread.currentThread().getName() + "	" + i + "	" + runTimes);

            }
            //唤醒
            flag = 3;
            cd3.signal();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    public void loopC(int runTimes) {
        lock.lock();
        try {
            //1.判断
            if (flag != 3) {
                cd3.await();
            }
            //打印
            for (int i = 0; i < 1; i++) {
                System.out.println(Thread.currentThread().getName() + "	" + i + "	" + runTimes);

            }
            //唤醒
            flag = 1;
            cd1.signal();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
}

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » JUC之分别打印A,B,C