【JAVA】线程 打印ABC

通过控制变量的方式

public class Test {

    public static  int num = 1;

    public static void main(String[] args) {

        Thread a = new Thread(new Runnable() {
            @Override
            public void run(){
                while (true){
                    if(num == 30){
                        break;
                    }
                    if(num % 3 == 1){
                        System.out.println("A");
                        num++;
                    }
                }
            }
        });
        Thread b = new Thread(new Runnable() {
            @Override
            public void run(){
                while (true){
                    if(num == 30){
                        break;
                    }
                    if(num % 3 == 2){
                        System.out.println("B");
                        num++;
                    }
                }
            }
        });
        Thread c = new Thread(new Runnable() {
            @Override
            public void run(){
                while (true){
                    if(num == 30){
                        break;
                    }
                    if(num % 3 == 0){
                        System.out.println("C");
                        num++;
                    }
                }
            }
        });
        a.start();
        b.start();
        c.start();
    }

}

通过用synchronized、wait、notifyAll实现

public class Test {

    public  static  int num = 1;

    public static class Pabc implements Runnable{
        private int id;
        private char[] ch = { 'C', 'A', 'B' };

        public Pabc(int id){
            this.id = id;
        }

        @Override
        public void run(){
            while (num <= 30){
                synchronized (Pabc.class){
                    if (num  % 3 == id){
                        System.out.println(ch[id]);
                        num++;
                        Pabc.class.notifyAll();
                    }else{

                        try {
                            Pabc.class.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        new Thread(new Pabc(1)).start();
        new Thread(new Pabc(2)).start();
        new Thread(new Pabc(0)).start();
    }

}

Condition 和 Lock 一起使用

    public  static  int num = 1;
    final  static  ReentrantLock lock = new ReentrantLock();
    final static Condition condition = lock.newCondition();

    public static class Pabc implements Runnable{
        private int id;
        private char[] ch = { 'C', 'A', 'B' };

        public Pabc(int id){
            this.id = id;
        }

        @Override
        public void run(){
            lock.lock();
            try {
                while (num <= 30){
                    if(num % 3 != id){
                        condition.await();
                    }else{
                        System.out.println(ch[id]);
                        num++;
                        condition.signalAll();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }

        }
    }

    public static void main(String[] args) {
        new Thread(new Pabc(1)).start();
        new Thread(new Pabc(2)).start();
        new Thread(new Pabc(0)).start();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java线程交替打印ABC的实现方式有多种。一种常见的方式是使用synchronized关键字和wait/notify方法来实现线程的阻塞和唤醒。每个线程打印完自己对应的字符后,调用notify方法唤醒下一个线程,并进入等待状态,等待下一个轮到自己打印字符。另一种方式是使用Lock和Condition来实现线程的阻塞和唤醒。每个线程打印完自己对应的字符后,调用signal方法唤醒下一个线程,并进入等待状态,等待下一个轮到自己打印字符。下面是一种使用synchronized和wait/notify方法的实现方式的示例代码: ```java public class PrintABC { private static final Object lock = new Object(); private static int count = 0; private static int maxCount = 10; public static void main(String[] args) { Thread t1 = new Thread(() -> { print("A", 0); }); Thread t2 = new Thread(() -> { print("B", 1); }); Thread t3 = new Thread(() -> { print("C", 2); }); t1.start(); t2.start(); t3.start(); } private static void print(String str, int target) { while (count < maxCount) { synchronized (lock) { while (count % 3 != target) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print(str); count++; lock.notifyAll(); } } } } ``` 以上代码使用了一个lock对象作为同步锁,count变量用于记录已经打印的字符数量。每个线程打印字符之前,都会先判断count是否是自己对应的值,如果不是,则调用wait方法进入等待状态,直到被唤醒后再进行判断。当一个线程打印完字符后,会调用notifyAll方法唤醒其他等待中的线程
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值