Java多线程:依次打印ABC

这篇博客探讨了如何在Java中使用synchronized和lock实现多线程环境下依次打印字母ABC的方案,详细解析了两种不同的并发控制方法。

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

1.多线程依次打印ABC - synchronized
package com.self.thread.moreThread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadTest {
    static int state = 0;
    //作为synchronized锁的同一对象
    private static Object 0 = new Object();
    
    public static void main(String[] args) {
        //线程池
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(new ThreadA());
        service.execute(new ThreadB());
        service.execute(new ThreadC());
        service.shutdown();
    }
    
    //线程A
    public static class ThreadA implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                synchronized (0) {
                    while (state % 3 != 0) {
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("--------第" + (i + 1) + "轮打印--------");
                    System.out.println("A");
                    state++;
                    o.notifyAll();
                }
            }
        }
    }
    
    //线程B
    public static class ThreadB implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                synchronized (o) {
                    while (state % 3 != 1) {
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("B");
                    state++;
                    o.notifyAll();
                }
            }
        }
    }
    
    //线程C
    public static class ThreadC implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                synchronized (o) {
                    while (state % 3 != 2) {
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("C");
                    state++;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    o.notifyAll();
                }
            }
        }
    }
}
2.多线程依次打印ABC - lock
package com.self.thread.moreThread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadTest2 {
    static int state = 0;
    private static Lock lock = new ReentrantLock();
    
    public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();
        service.submit(new ThreadA());
        service.submit(new ThreadB());
        service.submit(new ThreadC());
        service.shutdown();
    }
    
    //线程A
    public static class ThreadA implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 10; ) {
                lock.lock();
                if (state % 3 == 0) {
                    System.out.println("--------第" + (i + 1) + "轮打印--------");
                    System.out.println("A");
                    state++;
                    i++;
                }
                lock.unlock();
            }
        }
    }
    
    //线程B
    public static class ThreadB implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 10; ) {
                lock.lock();
                if (state % 3 == 1) {
                    System.out.println("B");
                    state++;
                    i++;
                }
                lock.unlock();
            }
        }
    }
    
    //线程C
    public static class ThreadC implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 10; ) {
                lock.lock();
                if (state % 3 == 2) {
                    System.out.println("C");
                    state++;
                    i++;
                }
                lock.unlock();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值