• 经典多线程问题(三)-子线程与主线程的循环打印


    1.0 使用 synchronized 关键字

    /**
     * @ClassName Question08
     * @Description: 子线程循环2次,主线程循环2次,这样循环10次;
     * @Author xtanb
     * @Date 2019/10/21
     * @Version V1.0
     **/
    public class Question08 {
        private volatile boolean flag = true;
    
        private synchronized void sub(){
            while (!flag){
                try{
                    this.wait();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            for(int i=0;i<2;i++){
                System.out.println("sub run "+i);
            }
            flag = false;
            this.notify();
        }
    
        private synchronized void main(){
            while(flag){
                try{
                    this.wait();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            for(int i=0;i<2;i++){
                System.out.println("main run "+i);
            }
            flag = true;
            this.notify();
        }
    
        public static void main(String[] args) {
            Question08 question08 = new Question08();
            new Thread(()->{
                for(int i=0;i<10;i++){
                    question08.sub();
                }
            }).start();
            for(int i=0;i<10;i++){
                question08.main();
            }
    
        }
    }

    2.0 使用ReentrantLock来完成

    package com.example.demo.study.questions;
    
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * @ClassName Question10
     * @Description: 子线程循环2次,主线程循环2次,这样循环10次;
     * @Author xtanb
     * @Date 2019/10/21
     * @Version V1.0
     **/
    public class Question10 {
        private volatile boolean flag = true;
        private Lock lock = new ReentrantLock();
        private Condition condition = lock.newCondition();
    
        public void sub(){
            try {
                lock.lock();
                while (!flag){
                    try{
                        condition.await();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                for(int i=0;i<2;i++){
                    System.out.println("sub run"+i);
                }
                flag = false;
                condition.signal();
            }finally {
                lock.unlock();
            }
        }
    
        public void main(){
            try{
                lock.lock();
                while (flag){
                    try{
                        condition.await();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                for(int i=0;i<2;i++){
                    System.out.println("main run"+i);
                }
                flag = true;
                condition.signal();
            }finally {
                lock.unlock();
            }
        }
    
        public static void main(String[] args) {
            Question10 question10 = new Question10();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for(int i=0;i<10;i++){
                        question10.sub();
                    }
                }
            }).start();
            for(int i=0;i<10;i++){
                question10.main();
            }
        }
    }
  • 相关阅读:
    CF 793 B (队列中的交换位置)
    WPF转换器
    WPF自定义控件指针仪表(依赖属性)
    面试可能会问到的问题
    返回局部变量是一个指向常量的字符串指针
    自定义c++二维动态数组
    1. 文件乱码 行远
    Nginx自启动脚本
    实验一密码引擎商用密码算法实现2交叉测试
    ARC117F Gateau
  • 原文地址:https://www.cnblogs.com/helloworldmybokeyuan/p/11715061.html
Copyright © 2020-2023  润新知