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

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();
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/helloworldmybokeyuan/p/11715061.html