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();
            }
        }
    }
  • 相关阅读:
    Linux启动报错missing operating system
    Linux tmp目录自动清理总结
    ORACLE口令管理
    oom_kill_process造成数据库挂起并出现found dead shared server
    Oracle shutdown immediate遭遇ORA-24324 ORA-24323 ORA-01089
    从flink-example分析flink组件(1)WordCount batch实战及源码分析
    从mysql高可用架构看高可用架构设计
    由mysql分区想到的分表分库的方案
    六边形效果
    迷你MVVM框架 avalonjs1.5.2 发布
  • 原文地址:https://www.cnblogs.com/helloworldmybokeyuan/p/11715061.html
Copyright © 2011-2022 走看看