zoukankan      html  css  js  c++  java
  • ABC三个线程交替打印10遍,要求A打印5次,B打印10次,C打印15次

    问题:

      ABC三个线程交替打印10遍,要求A打印5次,B打印10次,C打印15

     第一遍
    AAAAABBBBBBBBBBCCCCCCCCCCCCCCC
    第二遍
    AAAAABBBBBBBBBBCCCCCCCCCCCCCCC
    ...共打印10

    1.使用Sychronized+wait()+notify()的方式
    package com.yang.test;
    
    /**
     * ABC三个线程交替打印10遍,要求A打印5次,B打印10次,C打印15次
     * 第一遍
     * AAAAABBBBBBBBBBCCCCCCCCCCCCCCC
     * 第二遍
     * AAAAABBBBBBBBBBCCCCCCCCCCCCCCC
     * ...共打印10遍
     * @author yang yajun
     * @date 2020/12/2615:31
     */
    public class OrderThreadPrint {
    
        public static void main(String[] arg0){
            Print print = new Print();
    
            new Thread(()->{
                for (int i = 0; i < 10; i++) {
                    print.printA();
                }
            },"A").start();
            new Thread(()->{
                for (int i = 0; i < 10; i++) {
                    print.printB();
                }
            },"B").start();
            new Thread(()->{
                for (int i = 0; i < 10; i++) {
                    print.printC();
                }
            },"C").start();
        }
    }
    
    class Print{
        private int i = 1;
        public synchronized void  printA(){
            while (i%3!=1) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            for (int j = 0; j < 5; j++) {
                System.out.print(Thread.currentThread().getName() + i);
            }
            i++;
            notifyAll();
        }
        public synchronized void  printB(){
            while (i%3!=2) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            for (int j = 0; j < 10; j++) {
                System.out.print(Thread.currentThread().getName()+i);
            }
            i++;
            notifyAll();
        }
        public synchronized void  printC(){
            while (i%3!=0) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            for (int j = 0; j < 15; j++) {
                System.out.print(Thread.currentThread().getName() + i);
            }
            i++;
            notifyAll();
        }
    
    }
    
    2.使用Lock(Condition)+await()+signal()来实现
    package com.yang.test;
    
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * ABC三个线程交替打印10遍,要求A打印5次,B打印10次,C打印15次
     * 第一遍
     * AAAAABBBBBBBBBBCCCCCCCCCCCCCCC
     * 第二遍
     * AAAAABBBBBBBBBBCCCCCCCCCCCCCCC
     * ...共打印10遍
     * @author yang yajun
     * @date 2020/12/2615:31
     */
    public class OrderThreadPrintLock {
    
        public static void main(String[] arg0){
            PrintLk print = new PrintLk();
    
            new Thread(()->{
                for (int i = 0; i < 10; i++) {
                    print.printA();
                }
            },"A").start();
            new Thread(()->{
                for (int i = 0; i < 10; i++) {
                    print.printB();
                }
            },"B").start();
            new Thread(()->{
                for (int i = 0; i < 10; i++) {
                    print.printC();
                }
            },"C").start();
        }
    }
    
    class Print{
        private int i = 1;
        private Lock lock = new ReentrantLock();
        private Condition condition1 = lock.newCondition();
        private Condition condition2 = lock.newCondition();
        private Condition condition3 = lock.newCondition();
    
        public synchronized void  printA(){
            lock.lock();
            try {
                while (i!=1) {//判断,多线程通信,使用while代替if
                    try {
                        condition1.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            //干活 for (int j = 0; j < 5; j++) { System.out.print(Thread.currentThread().getName() + i); } i=2; condition2.signal();//通知 }finally { lock.unlock(); } } public synchronized void printB(){ lock.lock(); try { while (i!=2) { try { condition2.await(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int j = 0; j < 10; j++) { System.out.print(Thread.currentThread().getName()+i); } i=3; condition3.signal(); }finally { lock.unlock(); } } public synchronized void printC(){ lock.lock(); try { while (i!=3) { try { condition3.await(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int j = 0; j < 15; j++) { System.out.print(Thread.currentThread().getName() + i); } i=1; condition1.signal(); }finally { lock.unlock(); } } }

     总结:使用Synchronized和Lock都能实现,而Lock的优势是可以精准唤醒,不像Synchronized的只能notifyAll()

     
  • 相关阅读:
    BZOJ3992 [SDOI2015]序列统计 【生成函数 + 多项式快速幂】
    BZOJ3993 [SDOI2015]星际战争 【二分 + 网络流】
    BZOJ3325 [Scoi2013]密码 【manacher】
    BZOJ3534 [Sdoi2014]重建 【矩阵树定理】
    BZOJ3507 [Cqoi2014]通配符匹配 【哈希 + 贪心】
    BZOJ2285 [SDOI2011]保密 【01分数规划 + 网络流】
    BZOJ4556 [Tjoi2016&Heoi2016]字符串 【后缀数组 + 主席树 + 二分 + ST表】
    BZOJ4817 [Sdoi2017]树点涂色 【LCT + 线段树】
    BZOJ1195 [HNOI2006]最短母串 【状压dp】
    malloc的使用、用malloc动态分配内存以适应用户的需求的源代码实例
  • 原文地址:https://www.cnblogs.com/yayin/p/14193850.html
Copyright © 2011-2022 走看看