Java程序之多线程顺序打印 ABC

news/2024/6/30 4:09:18 标签: java
题目:

        按顺序打印 ABC ABC ABC ...。有这么一个多线程场景问题:有三个线程,线程1执行(输出A)完成之后线程2执行(输出B),线程2执行完之后线程3执行(输出C),线程3执行完成之后线程1执行...,整体循环50次,写程序实现

算法思路:

要求按照顺序打印,即A、B、C轮流打印,每个字母打印一次后换下一个字母继续打印,循环进行。

        1. 创建一个名为ThreadThreadp的类,其中包含一个flag变量,用于标记当前应该打印哪个字母。初始值为0,表示先打印A。
        2. 在ThreadThreadp类中定义三个同步方法printa()、printb()和printc(),分别用于打印A、B、C字母。这三个方法都需要传入一个ThreadThreadp对象作为参数。
        3. 在printa()方法中,首先判断flag是否为0,如果是,则打印A字母,并将flag设置为1,然后唤醒其他等待的线程。接着调用wait()方法让当前线程进入等待状态。
        4. 在printb()方法中,首先判断flag是否为1,如果是,则打印B字母,并将flag设置为2,然后唤醒其他等待的线程。接着调用wait()方法让当前线程进入等待状态。
        5. 在printc()方法中,首先判断flag是否为2,如果是,则打印C字母,并让当前线程休眠1秒,然后将flag设置为0,最后唤醒其他等待的线程。接着调用wait()方法让当前线程进入等待状态。
        6. 在main方法中,创建ThreadThreadp对象t,以及三个PrintA、PrintB、PrintC对象,分别传入t作为参数。然后创建三个线程t1、t2、t3,分别执行这三个PrintA、PrintB、PrintC对象的run()方法。最后启动这三个线程。
        7. 运行程序,可以看到按照顺序打印出ABC三个字母。

源代码:
Info.java
java">package Question9;

public class Info {
    private int flag = 0;
    public synchronized void printa() throws InterruptedException {
        while (true)
        {
            if(flag ==0)
            {
                System.out.println("A");
                flag = 1;
                notifyAll();
            }
            wait();
        }
    }
    public synchronized   void printb() throws InterruptedException {
        while (true)
        {
            if(flag ==1)
            {
                System.out.println("B");
                flag = 2;
                notifyAll();
            }
            wait();
        }
    }
    public synchronized void printc() throws InterruptedException {
        while (true) {
            if (flag == 2) {
                System.out.println("C");
                Thread.sleep(1000);
                flag = 0;
                notifyAll();
            }
            wait();
        }
    }

}

PrintA.java
java">package Question9;

class PrintA implements Runnable {
    private Info info;

    PrintA(Info info) {
        this.info = info;
    }

    @Override
    public void run() {
        try {
            info.printa();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
PrintB.java
java">package Question9;

class PrintB implements Runnable {

    private Info t;

    PrintB(Info t) {
        this.t = t;
    }

    @Override
    public void run() {
        try {
            t.printb();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
PrintC.java
java">package Question9;

class PrintC implements Runnable {
    private Info t;

    PrintC(Info t) {
        this.t = t;
    }

    @Override
    public void run() {
        try {
            t.printc();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Mian.java
java">package Question9;

public class Mian {
    public static void main(String[]args) throws InterruptedException
    {
        Info t = new Info();
        PrintA printA = new PrintA(t);
        PrintB printB = new PrintB(t);
        PrintC printC = new PrintC(t);
        Thread t1 = new Thread(printA);
        Thread t2 = new Thread(printB);
        Thread t3 = new Thread(printC);
        t1.start();
        t2.start();
        t3.start();

    }
}
运行结果: 


http://www.niftyadmin.cn/n/5534619.html

相关文章

【C语言】函数执行背后的秘密:函数栈帧的创建和销毁超详解

✨个人主页: 熬夜学编程的小林 💗系列专栏: 【C语言详解】 【数据结构详解】 目录 1. 什么是函数栈帧 2. 理解函数栈帧能解决什么问题呢? 3. 函数栈帧的创建和销毁解析 3.1 什么是栈? 3.2 认识相关寄存器和汇编指…

【PA交易】BackTrader(二): 同时使用tick和K线数据

前言 本文是BackTrader数据源系列的中篇。 文内会省略大量上篇文章的代码内容,直接阅读会产生轻微困惑。阅读前请务必完整理解并完成了Quickstart Guide - Backtrader 并阅读和实操过本系列第一篇: 【PA交易】BackTrader(一): 如何使用实时tick数据和…

详解 ClickHouse 的查询优化

一、单表查询 1. 使用 prewhere 替代 where prewhere 和 where 语句的作用相同,都是用来过滤数据prewhere 和 where 语句的不同在于: prewhere 只支持 MergeTree 族系列引擎的表prewhere 首先会读取指定的列数据来判断数据过滤,等待数据过滤…

GD32 串口接受异常的几个原因

前面我们介绍过GD32 485发送时出现异常的最常见原因,有小伙伴反馈想要知道GD32 串口接受异常的可能原因,今天我们就来安排。 一、波特率异常导致收发出错 我们知道,串口是异步通讯接口,通讯双方或者多方都需要工作在相同波特率下…

Kotlin基础——Typeclass

高阶类型 如在Iterable新增泛型方法时 interface Iterable<T> {fun filter(p: (T) -> Boolean): Iterable<T>fun remove(p: (T) -> Boolean): Iterable<T> filter { x -> !p(x) } }对应的List、Set实现上述方法时仍需要返回具体的类型 interfac…

Java中的反射编程实用指南

Java中的反射编程实用指南 大家好&#xff0c;我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编&#xff0c;也是冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01;今天&#xff0c;我们将深入探讨Java中的反射编程。反射是Java提供的一种强大机制&am…

DDei在线设计器-API-DDeiFile

DDeiFile DDeiFile是代表一个设计文件&#xff0c;一个文件含有多个DDeiSheet(页签)。   DDeiFile实例包含了一个文件的所有数据&#xff0c;在获取后可以通过它访问其他内容。DDeiEditor中的files属性记录了当前打开的文件列表。 一个DDeiEditor实例至少包含一个DDeiFile实例…

创客项目秀|基于XIAO ESP32S3 sense 的小型相机

在这个科技飞速发展的时代&#xff0c;DIY&#xff08;Do It Yourself&#xff09;文化正成为连接创新与日常生活的桥梁&#xff0c;今天小编给大家带来了来自麻省理工学院的Arnov Sharma 的基于XIAO ESP32S3 sense的小型相机项目&#xff0c;该相机拥有一个圆形的触摸屏幕可以…