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#实现窗口最小化到系统托盘
    C#中访问私有成员--反射
    不要在构造函数中调用可重写的方法
    链表解决约瑟夫环问题
    C数据结构(文件操作,随机数,排序,栈和队列,图和遍历,最小生成树,最短路径)程序例子
    java this,super简单理解
    数据与计算机通信习题
  • 原文地址:https://www.cnblogs.com/helloworldmybokeyuan/p/11715061.html
Copyright © 2011-2022 走看看