Java多线程循环打印ABC等问题

前言

有很多中写法,希望可以慢慢都写出来。

1、线程共享类对象:依靠AtomicInteger是线程安全的

将 AtomicInteger currentCount 作为线程共享。


import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 三个线程按次序轮流打印a,b,c AtomicInteger 实现
 *
 */
public class AtomicIntegerThread implements Runnable {
    private AtomicInteger currentCount = new AtomicInteger(0);

    private static final Integer MAX_COUNT = 6;

    private static String[] chars = {"a", "b", "c"};

    private String name;

    public AtomicIntegerThread(String name,AtomicInteger currentCount) {
        this.name = name;
        this.currentCount = currentCount;
    }

    @Override
    public void run() {
        while (currentCount.get() < MAX_COUNT) {
            if (this.name.equals(chars[currentCount.get() % 3])) {
                printAndPlusOne(this.name);
            }
        }
    }

    public void printAndPlusOne(String content) {
        System.out.print(content);
        currentCount.getAndIncrement();
    }

    public static void main(String[] args) {
        
        AtomicInteger currentCount = new AtomicInteger(0);

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 3, 20, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>());
        threadPoolExecutor.execute(new AtomicIntegerThread("a",currentCount));
        threadPoolExecutor.execute(new AtomicIntegerThread("b",currentCount));
        threadPoolExecutor.execute(new AtomicIntegerThread("c",currentCount));
        threadPoolExecutor.shutdown();
        
    }
}

这里一定要注意有一个坑,如果线程池核心线程数少于3个,1个或者2个,上述代码会有问题,因为线程池的队列是无界队列,多余核心线程数的任务会被放到队列中,这样循环打印的时候,有一些线程不是在运行中的,是在队列里的,运行中的线程会被阻塞,导致相互等待,死锁问题出现。所以线程池的核心线程数必须等于大于3个,这样保证有3个线程一直在运行。

2、volatile静态变量控制

还有一种就是将某个变量设置为静态,这样线程本身也共享该变量,则可以对其操作。但要注意给这个静态变量加上volatile字段,这个字段可以帮助多线程中的可见性。代码如下:

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 三个线程按次序轮流打印a,b,c
 *
 */
public class StaticVarThread implements Runnable {

        private static volatile Integer currentCount = 0;


    private static final Integer MAX_COUNT = 6;

    private static String[] chars = {"a", "b", "c"};

    private String name;

    public StaticVarThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        while (currentCount < MAX_COUNT) {
            try {
                while (this.name.equals(chars[currentCount % 3]) && currentCount < MAX_COUNT) {
                    printAndPlusOne(this.name);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
        }
    }

    public void printAndPlusOne(String name) {
        System.out.println(name + "\t" + currentCount);
        currentCount++;
    }

    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 3, 20, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>());
        threadPoolExecutor.execute(new StaticVarThread("a"));
        threadPoolExecutor.execute(new StaticVarThread("b"));
        threadPoolExecutor.execute(new StaticVarThread("c"));
        threadPoolExecutor.shutdown();
    }
}


3、synchronized对象加锁

利用一个计数来除线程个数得到余数从而控制线程输出,并且利用synchronized、一个Object的wait()和notify()方法来对线程的阻塞和唤醒,这里的技巧可以看看。

  • wait()
    阻塞当前线程,等待被释放,继续执行当前线程
  • notify()
    唤醒监视该对象的第一个线程,notifyAll()唤醒监视该对象的所有线程。

代码:


    static volatile int state; // 线程共有,判断所有的打印状态
    static final Object t = new Object(); // 线程共有,加锁对象

/**
     * 改装了一下
     */
    @Test
    public void mainSix() {
        state = 0;
        // 需要循环的次数
        int n = 3;
        int num = 2;

        new MyThread("A", 0, n, num).start();
        new MyThread("B", 1,  n, num).start();
    }


    class MyThread extends Thread {
        String name;
        int which; // 标示符:0:打印A;1:打印B;2:打印C
        int n; // 0:打印次数
        int num; // 循环周期


        public MyThread(String name,int which,int n,int num) {
            this.name = name;
            this.which = which;

            this.n = n;
            this.num = num;
        }

        @Override
        public void run() {
            for (int i = 0; i < n; i++) {
                synchronized (t) {
                    while (state % num != which) {
                        try {
                            t.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print(name); // 执行到这里,表明满足条件,打印
                    state++;
                    t.notify(); // 调用notifyAll方法
                }
            }
        }
    }

    

4、condition 与lock的结合

synchronized关键字与wait()和notify/notifyAll()方法相结合可以实现等待/通知机制,ReentrantLock类当然也可以实现,但是需要借助于Condition接口与lock 对象的newCondition() 方法,condition 是lock对象所创建的,在Condition中,用await()替换wait(),用signal()替换notify(),用signalAll()替换notifyAll(),传统线程的通信方式,Condition都可以实现,其突出的优点有:

  • Condition能够支持不响应中断,而通过使用Object方式不支持;
  • Condition能够支持多个等待队列(new 多个Condition对象),而Object方式只能支持一个;
  • Condition能够支持超时时间的设置,而Object不支持
  • 调用Condition的await()和signal()方法,都必须在lock保护之内,就是说必须在lock.lock()和lock.unlock之间才可以使用

使用场景:多个condition的唤醒机制,比如多个生产者同时生产,多个消费者同时消费,需要两个condition来唤醒,或者是有先后顺序执行的线程,比如顺序打印ABC。
Condition是基于AQS同步机制的同步方法。 AbstractQueuedSynchronizer(AQS)。



import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionTwo extends Thread {

    private Lock lock;

    private int count;

    private String str;
    private int flag;
    Condition A;
    Condition B;

    public ConditionTwo(String str, int flag, int count, Lock lock, Condition A, Condition B) {
        this.str = str;
        this.flag = flag;
        this.A = A;
        this.B = B;
        this.count = count;
        this.lock = lock;
    }

    @Override
    public void run() {
        try {
            lock.lock();
            while (count<2) {
                System.out.println(" str = " + str+" count =" +count);
                count++;
                // 唤醒 B 线程
                B.signal();
                // 暂停自己进程
                A.await();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            B.signal();
            lock.unlock();
        }
    }


    public static void main(String[] args) throws InterruptedException {

        Lock lock = new ReentrantLock();

        int count = 0;

        Condition A = lock.newCondition();
        Condition B = lock.newCondition();
        Condition C = lock.newCondition();

        new ConditionTwo("a", 0,count,lock, A, B).start();
        
        // 必须要加 这个限制,不然可能不是a 先执行
        
        Thread.sleep(500);
        
        new ConditionTwo("b", 1,count,lock,  B, C).start();
        new ConditionTwo("c", 2,count,lock,  C, A).start();
        
    }
}


参考博客



  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
为了实现三个线程循环打印ABC,可以使用Synchronized同步方法和Object的wait()和notify()方法。首先,创建三个线程A、B、C,并设置它们的打印次数为10。然后,通过使用三个对象锁a、b、c来控制线程的执行顺序。A线程首先获得c对象锁,打印A后释放c对象锁,并通过notify()方法唤醒B线程。B线程等待a对象锁,获取到a对象锁后打印B,并释放a对象锁,然后通过notify()方法唤醒C线程。C线程等待b对象锁,获取到b对象锁后打印C,并释放b对象锁,并通过notify()方法唤醒A线程。这样就实现了三个线程循环打印ABC的需求。 以下是一个示例代码: ```java class PrintThread implements Runnable { private static final Object a = new Object(); private static final Object b = new Object(); private static final Object c = new Object(); private String name; public PrintThread(String name) { this.name = name; } @Override public void run() { for (int i = 0; i < 10; i++) { synchronized (name) { try { switch (name) { case "A": synchronized (c) { System.out.print("A"); c.notify(); } name.wait(); break; case "B": synchronized (a) { System.out.print("B"); a.notify(); } name.wait(); break; case "C": synchronized (b) { System.out.print("C"); b.notify(); } name.wait(); break; } } catch (InterruptedException e) { e.printStackTrace(); } } } } } public class Main { public static void main(String[] args) { Thread threadA = new Thread(new PrintThread("A")); Thread threadB = new Thread(new PrintThread("B")); Thread threadC = new Thread(new PrintThread("C")); threadA.start(); threadB.start(); threadC.start(); } } ``` 通过以上代码,三个线程将按照ABCABCABC的顺序循环打印10次。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [多线程交替打印ABC的多种实现方法](https://blog.csdn.net/xiaokang123456kao/article/details/77331878)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [三个线程轮流打印ABC](https://blog.csdn.net/yu1336199790/article/details/118725454)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值