加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

经典多线程问题-轮流打印字母和数字

发布时间:2020-12-15 07:19:26 所属栏目:Java 来源:网络整理
导读:1.0 synchronized package com.example.demo.study.questions; /** * @ClassName Question13 * @Description: 经典多线程问题-轮流打印字母和数字 * @Author xtanb * @Date 2019/10/22 * @Version V1.0 * */ public class Question13 { private volatile boo

1.0 synchronized

package com.example.demo.study.questions;

/**
 * @ClassName Question13
 * @Description: 经典多线程问题-轮流打印字母和数字
 * @Author xtanb
 * @Date 2019/10/22
 * @Version V1.0
 **/
public class Question13 {
    private volatile boolean flag = true;
    private int count = 1;

    public synchronized void printNum(){
        while (!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.print(2*count-1);
        System.out.print(2*count);
        flag = false;
        this.notify();
    }

    public synchronized void printABC(){
        while (flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.print((char)(‘A‘+count-1));
        count++;
        flag = true;
        this.notify();
    }


    public static void main(String[] args) {
        Question13 question13 = new Question13();
        new Thread(()->{
            for(int i=0;i<26;i++){
                question13.printNum();
            }
        }).start();
        new Thread(()->{
            for(int i=0;i<26;i++){
                question13.printABC();
            }
        }).start();
    }
}

2.0 ReentranLock

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 Question14
 * @Description: 经典多线程问题-轮流打印字母和数字
 * @Author xtanb
 * @Date 2019/10/22
 * @Version V1.0
 **/
public class Question14 {

    private volatile boolean flag = true;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    private int num = 1;

    public void printNum(){
        try {
            lock.lock();
            while (!flag){
                try{
                    condition.await();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.print(num*2-1);
            System.out.print(num*2);

            flag = false;
            condition.signal();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void printABC(){
        try {
            lock.lock();
            while (flag){
                try{
                    condition.await();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.print((char)(‘A‘+num-1));
            num++;
            flag = true;
            condition.signal();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        Question14 question14 = new Question14();
        new Thread(()->{
           for(int i=0;i<26;i++){
               question14.printNum();
           }
        }).start();
        new Thread(()->{
            for(int i=0;i<26;i++){
                question14.printABC();
            }
        }).start();
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读