三个线程循环顺序打印

题目描述

三个线程循环打印A、B、C。

解题思路

主要考虑的是线程间同步问题,可以通过线程间等待/通知模型来解决顺序性。

代码实现

方案一

通过thread.join()来控制线程间的顺序性,表示在当前线程中执行另一个线程的thread.join()方法,可以等待另一个线程执行完成之后,当前线程才执行,通过这样的方式来控制两个线程的先后顺序。

/**
 * @author yangnk
 * @desc
 * @date 2023/08/26 15:30
 **/
public class JoinTest {
    public static void main(String[] args) throws InterruptedException {
        while (true) {
            Thread thread1 = new Thread(new MyTask1());
            Thread thread2 = new Thread(new MyTask2(thread1));
            Thread thread3 = new Thread(new MyTask3(thread2));

            thread1.start();
            thread2.start();
            thread3.start();

            thread3.join();
        }

    }
}

class MyTask1 implements Runnable {

    @Override
    public void run() {
        System.out.println("A");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class MyTask2 implements Runnable {

    Thread thread;

    public MyTask2(Thread thread) {
        this.thread = thread;
    }

    @Override
    public void run() {
        try {
        thread.join();
        System.out.println("B");
        Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class MyTask3 implements Runnable {

    Thread thread;

    public MyTask3(Thread thread) {
        this.thread = thread;
    }

    @Override
    public void run() {
        try {
            thread.join();
            System.out.println("C");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

方案二

通过wait()/notiyf()来实现等待/通知模型,首先线程A获取锁后执行线程A中的打印逻辑,然后线程A调用了lock对象的notifyAll()方法进入释放lock对象,线程B获取到锁后执行线程B中的打印逻辑,然后线程B调用了lock对象的notifyAll()方法进入释放lock对象,线程C获取到锁后执行线程C中的打印逻辑,然后线程C调用了lock对象的notifyAll()方法进入释放lock对象。

适合该场景的等待/通知模型:

(1) 获取全局对象锁。
(2) 如果条件不满足,那么调用对象的wait()方法,此时会释放锁。
(3) 竞争到锁并且条件满足,则执行对应的逻辑。
(4) 释放全局对象锁,其他线程可以开始竞争锁。

synchronized(lock){
    while(条件不满足){
        lock.wait()
    }
    业务逻辑
    lock.notiyfAll()
}

代码实现:

/**
 * @author yangnk
 * @desc
 * @date 2023/08/27 09:40
 **/
public class WaitNotifyTest {
    private static Object lock = new Object();
    private static int num = 0;


    public static void main(String[] args) {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    try {
                        while (true) {
                            while (num % 3 != 0) {
                                lock.wait();
                            }
                            Thread.sleep(1000);
                            num++;
                            System.out.println("A");
                            lock.notifyAll();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    try {
                        while (true) {
                            while (num % 3 != 1) {
                                lock.wait();
                            }
                            Thread.sleep(1000);
                            num++;
                            System.out.println("B");
                            lock.notifyAll();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    try {
                        while (true) {
                            while (num % 3 != 2) {
                                lock.wait();
                            }
                            Thread.sleep(1000);
                            num++;
                            System.out.println("C");
                            lock.notifyAll();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread1.start();
        thread2.start();
        thread3.start();

    }
}

方案三

通过ReentrantLock+Condition方法来顺序打印,其中Condition可以实现全局锁的多条件控制。

/**
 * @author yangnk
 * @desc
 * @date 2023/08/27 00:16
 **/
public class ReentrantLockLockTest {
    private static ReentrantLock lock = new ReentrantLock();
    private static Condition condition1 = lock.newCondition();
    private static Condition condition2 = lock.newCondition();
    private static Condition condition3 = lock.newCondition();
    private static int num = 0;

    public static void main(String[] args) {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    lock.lock();
                    try {
                        while (num % 3 != 0) {
                            condition1.await();
                        }
                        Thread.sleep(1000);
                        System.out.println("A");
                        num++;
                        condition2.signal();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    lock.lock();
                    try {
                        while (num % 3 != 1) {
                            condition2.await();
                        }
                        Thread.sleep(1000);
                        System.out.println("B");
                        num++;
                        condition3.signal();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            }
        });
        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    lock.lock();
                    try {
                        while (num % 3 != 2) {
                            condition3.await();
                        }
                        Thread.sleep(1000);
                        System.out.println("C");
                        num++;
                        condition1.signal();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            }
        });

        thread1.start();
        thread2.start();
        thread3.start();
    }
}

本文由博客一文多发平台 OpenWrite 发布!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yangnk42

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值