zoukankan      html  css  js  c++  java
  • java多线程题目01

    题目:两个线程交替顺序打印,一个输出正整数1到52,一个输出a-zA-Z,

    例如 - 1a2b3c4d5e6f7g8h9i10j11k12l13m14n15o16p17q18r19s20t21u22v23w24x25y26z

    27A28B29C30D31E32F33G34H35I36J37K38L39M40N41O42P43Q44R45S46T47U48V49W50X51Y52Z

    一、使用volatile

    二、使用Semaphore

    三、思路: 一个线程等待,一个线程唤醒,始终只有一个线程执行

    a. volatile

    public class NumberDemo {
        static class Inner {
            private volatile boolean flag = true;
            private int count = 1;
            private int index = 1;
    
            public synchronized void printNum() {
                try {
                    while (!flag) {
                        this.wait();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.print(count++);
                flag = false;
                this.notify();
            }
    
            public synchronized void printChar() {
                try {
                    while (flag) {
                        this.wait();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (index < 27) {
                    System.out.print((char) (index + 'a' - 1));
                } else {
                    System.out.print((char) (index + 'A' - 27));
                }
                index++;
                flag = true;
                this.notify();
            }
        }
    
        public static void main(String[] args) {
            Inner inner = new Inner();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 52; i++) {
                        inner.printNum();
                    }
                }
            }).start();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 52; i++) {
                        inner.printChar();
                    }
                }
            }).start();
        }
    }
    

      b. Semaphore

    public class NumberSemaphoreDemo {
    
        static class Inner {
    
            private Semaphore semaphore = new Semaphore(1);
    
            private int count = 1;
            private int index = 1;
    
            public synchronized void printNum() {
                try {
                    semaphore.acquire();
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.print(count++);
                semaphore.release();
                this.notify();
            }
    
            public synchronized void printChar() {
                try {
                    semaphore.acquire();
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (index < 27) {
                    System.out.print((char) (index + 'a' - 1));
                } else {
                    System.out.print((char) (index + 'A' - 27));
                }
                index++;
                semaphore.release();
                this.notify();
            }
        }
    
        public static void main(String[] args) {
            NumberDemo.Inner inner = new NumberDemo.Inner();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 52; i++) {
                        inner.printNum();
                    }
                }
            }).start();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 52; i++) {
                        inner.printChar();
                    }
                }
            }).start();
        }
    }
    

      

  • 相关阅读:
    Excel导出采用mvc的ExcelResult继承遇到的问题Npoi导出
    Excel导出采用mvc的ExcelResult继承遇到的问题
    word模板导出的几种方式:第三种:标签替换(DocX组件读取与写入Word)
    word模板导出的几种方式:第二种:C#通过模板导出Word(文字,表格,图片) 占位符替换
    word模板导出的几种方式:第一种:占位符替换模板导出(只适用于word中含有表格形式的)
    vue 学习链接地址
    创建作业(JOB)
    html5 浏览文件
    Guava monitor
    Spring Rabbitmq HelloWorld实例
  • 原文地址:https://www.cnblogs.com/maduar/p/10605674.html
Copyright © 2011-2022 走看看