玩碎JAVA之Java 多线程顺序打印ABC

本文介绍了一种使用Java多线程实现顺序打印字符A、B、C各10次的方法。通过自定义对象锁和布尔标志控制线程间的同步,确保了打印顺序正确,同时讨论了性能问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2022年1月17日更新一版 

public class Test2 implements Runnable {
    int i = 0;

    private synchronized void printChar() {
        if(i==0) {
            System.out.println("A");
            i++;
        } else if(i==1) {
            System.out.println("B");
            i++;
        } else {
            System.out.println("C");
            i = 0;
        }
    }


    public static void main(String[] args) {
        int count = 100;
        Test2 test2 = new Test2();
        for (int i = 0; i < count; i++) {
            Thread threadA = new Thread(test2, "tA");
            Thread threadB = new Thread(test2, "tB");
            Thread threadC = new Thread(test2, "tC");

            threadA.start();
            threadB.start();
            threadC.start();
        }
    }

    @Override
    public void run() {
        printChar();
    }
}

以下为2019-07-28 21:27:59写的,太业余!!

之前看了个题目,利用多线程顺序打印10次ABC,方法有多种,以下也是一种思路。

不过,这样写,循环多少次,就需要创建多少个线程,性能不是很好

package com.thread.chapter1;

public class PrintABC {
    Object lock = new Object();
    boolean isA = true, // 第一次要先打印A,所以默认值为true
            isB = false, isC = false;
    public static void main(String[] args) throws InterruptedException {
        PrintABC printABC = new PrintABC();
        for (int i = 0; i < 10; i++) {
            Thread threadA = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        printABC.printA(Thread.currentThread().getName());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });

            Thread threadB = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        printABC.printB(Thread.currentThread().getName());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });

            Thread threadC = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        printABC.printC(Thread.currentThread().getName());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });

            threadA.start();
            Thread.sleep(100); // 如果不加sleep 100,容易打印不够10次,
                                     // 竞争太厉害,容易造成死锁等待(即isA/isB/isC都为false)
            threadB.start();
            Thread.sleep(100);
            threadC.start();
            Thread.sleep(100);
        }
        System.out.println("main");
    }

    private void printA(String threadName) throws InterruptedException {
        synchronized (lock) {
            while (!isA) {
                lock.wait(); //wait/notify要配合synchronized使用
            }
            System.out.println("A:: "+threadName);
            isA = false;
            isB = true;
            lock.notify();
        }
    }

    private void printB(String threadName) throws InterruptedException {
        synchronized (lock) {
            while (!isB) {
                lock.wait();
            }
            System.out.println("B:: "+threadName);
            isB = false;
            isC = true;
            lock.notify();
        }
    }

    private void printC(String threadName) throws InterruptedException {
        synchronized (lock) {
            while (!isC) {
                lock.wait();
            }
            System.out.println("C:: "+threadName);
            isC = false;
            isA = true;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值