创建10个线程循环打印ABC(两种方法) 多线程(5)

本文详细介绍了如何在Python中通过两种不同的方法创建10个线程,每个线程循环打印字母A、B、C。内容包括线程同步的概念,以及使用threading模块和Queue实现线程间协调的实例代码。
摘要由CSDN通过智能技术生成
package Lesson5;
//三个线程打印十次 ABC ABC
public class SequencePrint2 {
    private  volatile  static String[] INFOS={"A","B","C"};
    private  volatile  static int index;   //数组下标
    public static void main(String[] args) {
        new Thread(new Tast("A")).start();
        new Thread(new Tast("B")).start();
        new Thread(new Tast("C")).start();
    }
    static class Tast implements Runnable {
        private String name;

        public Tast(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            try {
                for (int i = 0; i <10; i++) {
                    synchronized (SequencePrint2.class) {
                        while (!name.equals(INFOS[index])) {
                            SequencePrint2.class.wait();
                        }
                        //name=INFOS[index]
                        System.out.println(name);
                        if(index==INFOS.length-1){
                            //遇到C就换行
                            System.out.println();
                        }
                        //下一个要打印的下标,因为数组循环打印(循环队列)加1取模数组的长度
                        index = (index + 1) % INFOS.length;
                        SequencePrint2.class.notifyAll();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

package Lesson5;
//三个线程打印十次 ABC ABC
public class SequencePrint {
    private  volatile  static String index="A";  //当前打印的字符串
    public static void main(String[] args) {
        Thread a=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for(int i=0;i<=9;i++){
                        synchronized (SequencePrint.class){
                            while (!index.equals("A")){
                                SequencePrint.class.wait();
                            }
                            System.out.println(index);
                            index="B";
                            SequencePrint.class.notifyAll();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread b=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for(int i=0;i<=9;i++){
                        synchronized (SequencePrint.class){
                            while (!index.equals("B")){
                                SequencePrint.class.wait();
                            }
                            System.out.println(index);
                            index="C";
                            SequencePrint.class.notifyAll();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread c=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for(int i=0;i<=9;i++){
                        synchronized (SequencePrint.class){
                            while (!index.equals("C")){
                                SequencePrint.class.wait();
                            }
                            System.out.println(index);
                            index="A";
                            SequencePrint.class.notifyAll();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        a.start();
        b.start();
        c.start();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值