Java多线程应用循环输出ABC方式

 更新时间:2025年05月16日 14:08:16   作者:二六八  
这篇文章主要介绍了Java多线程应用循环输出ABC方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

Java 多线程应用循环输出ABC

有三个线程ID分别是A、B、C,请用多线编程实现,在屏幕上循环打印10次ABCABC

请补充以下代码

public class Test { 
    public static void main(String[] args) { 
        MajusculeABC maj = new MajusculeABC(); 
        Thread t_a = new Thread(new Thread_ABC(maj , 'A')); 
        Thread t_b = new Thread(new Thread_ABC(maj , 'B')); 
        Thread t_c = new Thread(new Thread_ABC(maj , 'C')); 
        t_a.start(); 
        t_b.start(); 
        t_c.start(); 
    } 
} 
class MajusculeABC { 
请补充代码
} 
class Thread_ABC implements Runnable {
请补充代码
}

答案如下

public class ThreadTest {
    public static void main(String[] args) throws InterruptedException {
        MajusculeABC maj = new MajusculeABC();
        Thread t_a = new Thread(new Thread_ABC(maj , 'A'));
        Thread t_b = new Thread(new Thread_ABC(maj , 'B'));
        Thread t_c = new Thread(new Thread_ABC(maj , 'C'));
        t_a.start();
        t_b.start();
        t_c.start();
    }
    private static class MajusculeABC {
        //        请补充代码
        public MajusculeABC() {
        }

        private int a = 1;

        private synchronized void print(int index, char s) throws InterruptedException {
            do {
                if (s == 'A' && a == 1) {
                    a++;
                    System.out.println(index+"---" + s);
                    notifyAll();
                    break;
                } else if (s == 'B' && a == 2) {
                    a++;
                    System.out.println(index+"---" + s);
                    notifyAll();
                    break;
                } else if (s == 'C' && a == 3) {
                    a = 1;
                    System.out.println(index+"---" + s);
                    notifyAll();
                    break;
                } else {
                    wait();
                }

            } while (true);

        }
    }

    private static class Thread_ABC implements Runnable {

        private char s;
        private MajusculeABC majusculeABC;

        //        请补充代码
        public Thread_ABC(MajusculeABC majusculeABC, char s) {
            this.majusculeABC = majusculeABC;
            this.s = s;
        }

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                try {
                    majusculeABC.print(i, s);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

输出如下:

0---A
0---B
0---C
1---A
1---B
1---C
2---A
2---B
2---C
3---A
3---B
3---C
4---A
4---B
4---C
5---A
5---B
5---C
6---A
6---B
6---C
7---A
7---B
7---C
8---A
8---B
8---C
9---A
9---B
9---C

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • spring-boot通过@Scheduled配置定时任务及定时任务@Scheduled注解的方法

    spring-boot通过@Scheduled配置定时任务及定时任务@Scheduled注解的方法

    这篇文章主要介绍了spring-boot通过@Scheduled配置定时任务,文中还给大家介绍了springboot 定时任务@Scheduled注解的方法,需要的朋友可以参考下
    2017-11-11
  • Java实现多个单张tif文件合并成一个多页tif文件

    Java实现多个单张tif文件合并成一个多页tif文件

    业务部门需要将多个单张的tiff文件,合并成一个多页的tiff文件,本文就来介绍一下如何实现,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • SpringBoot快速集成Logback日志组件

    SpringBoot快速集成Logback日志组件

    本文主要介绍了SpringBoot快速集成Logback日志组件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • JavaWeb中的filter过滤敏感词汇案例详解

    JavaWeb中的filter过滤敏感词汇案例详解

    敏感词、文字过滤是一个网站必不可少的功能,本篇文章主要介绍了JavaWeb中的filter过滤敏感词汇案例,具有一定的参考价值,有需要的可以了解一下,
    2016-11-11
  • 使用Java进行FreeMarker的web模板开发的基础教程

    使用Java进行FreeMarker的web模板开发的基础教程

    这篇文章主要介绍了使用Java进行FreeMarker模板引擎开发的基础教程,文中针对FreeMarker的网页标签用法给出了一些例子,需要的朋友可以参考下
    2016-03-03
  • java返回json请求中文变成问号的问题及解决

    java返回json请求中文变成问号的问题及解决

    这篇文章主要介绍了java返回json请求中文变成问号的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • 详解Spring ApplicationContext加载过程

    详解Spring ApplicationContext加载过程

    这篇文章主要介绍了Spring ApplicationContext加载过程的相关资料,帮助大家更好的理解和学习使用spring框架,感兴趣的朋友可以了解下
    2021-03-03
  • MyBatis-Plus allEq()的用法详解

    MyBatis-Plus allEq()的用法详解

    这篇文章主要介绍了MyBatis-Plus allEq()的用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Spring Boot事务配置详解

    Spring Boot事务配置详解

    这篇文章主要介绍了Spring Boot事务配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Java基础之二叉搜索树的基本操作

    Java基础之二叉搜索树的基本操作

    发现许多小伙伴还不清楚Java二叉搜索树的基本操作,今天特地整理了这篇文章,文中有非常详细的代码示例,对正在学习Java的小伙伴很有帮助,需要的朋友可以参考下
    2021-05-05

最新评论