zoukankan      html  css  js  c++  java
  • 经典多线程问题(三)-子线程与主线程的循环打印

    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();
            }
        }
    }
  • 相关阅读:
    C#中的控件命名规范
    c# 执行javascript 脚本 并获得变量值 (winform与javascript交互 )
    C# Socket 实现的淘宝秒杀器(抢拍器)
    ASP函数 获取域名解析后的IP 获取远程网页的HTML代码
    C# 使用17方法编写Hello World程序(语法深度)
    框架内数据的打印。。。。
    有点厌恶写程序.....
    漂流的自白
    Asp.net中的页面乱码的问题
    在js 中根据 url 获取其参数
  • 原文地址:https://www.cnblogs.com/helloworldmybokeyuan/p/11715061.html
Copyright © 2011-2022 走看看