两个线程交替打印A1B2C3 ... 的几种实现方式

sun flower

有以下需求:
两个线程,需要打印字母和数字,格式A1B2C3 …
这个问题涉及到线程的等待,唤醒,线程间通信等知识。
下面看看实现代码:

方法1:LockSupport

import java.util.concurrent.locks.LockSupport;

/**
 * @author liming
 * @date 2020/10
 * @description 交替打印 A1B2C3 ...
 */
public class AlternatePrint {

    static Thread t1 = null, t2 = null;

    public static void main(String[] args) {
        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();

        t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < aC.length; i++) {
                    // 起始先打印一个字母
                    System.out.println(aC[i]);
                    // 打印完唤醒t2打印数字
                    LockSupport.unpark(t2);
                    // 自己阻塞,等待唤醒
                    LockSupport.park();
                }
            }
        });

        t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < aI.length; i++) {
                    // 起始先阻塞等待
                    LockSupport.park();
                    // 被唤醒后打印数字
                    System.out.println(aI[i]);
                    // 唤醒t1
                    LockSupport.unpark(t1);
                }
            }
        });

        t1.start();
        t2.start();
    }

}

方法2:synchronized

import org.junit.Test;

/**
 * @author liming
 * @date 2020/10/14
 * @description 交替打印 A1B2C3 ...
 */

public class AlternatePrint {

    static Thread t1 = null, t2 = null;

    /**
     * 使用 synchronized
     */
    @Test
    public void alternatePrint() {
        Object lock = new Object();
        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();

        t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < aC.length; i++) {
                    synchronized (lock) {
                        System.out.println(aC[i]);
                        lock.notify();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

        t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < aI.length; i++) {
                    synchronized (lock) {
                        System.out.println(aI[i]);
                        lock.notify();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

        t1.start();
        t2.start();
    }

}

方法3: BlockingQueue 实现

利用阻塞队列的阻塞特性也可实现交替打印。
实现过程:
初始两个阻塞队列,容量为1,一个用来存储字母,一个用来存储数字。
初始向字母队列加入第一个字母,
线程t1,循环内到字母队列取数据,如为空则等待,取到数据后打印。向数字队列加数据-唤醒线程t2。下次循环队列为空会等待 - t2向字母队列加数据 - 唤醒。如此往复。
线程t2,循环从数字阻塞队列取数据,如队列为空则等待。如取到数据-打印。向字母阻塞队列加数据 - 唤醒阻塞的t1。下次循环阻塞式取数据 - 被唤醒。如此往复。
其实原理还是两线程阻塞,互相轮流唤醒的过程。只不过阻塞和唤醒,线程间间通信交给了阻塞队列。
阻塞队列底层是用到 ReentrantLockReentrantLock 底层是 AQSAQS 底层也应用到CAS + LockSupport

import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @author liming
 * @date 2020/10/16
 * @description
 */
public class AlternatePrint {

    private Thread t1, t2;
    private final BlockingQueue<Character> numBlockingQueue = new ArrayBlockingQueue<>(1);
    private final BlockingQueue<Character> alphabetBlockingQueue = new ArrayBlockingQueue<>(1);
    private final char[] nums = "1234567".toCharArray();
    private final char[] alphabets = "ABCDEFG".toCharArray();

    /**
     * BlockingQueue 实现
     */
    @Test
    public void alternatePrint() throws InterruptedException, IOException {
        alphabetBlockingQueue.add(alphabets[0]);

        t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < alphabets.length; i++) {
                    try {
                        // 返回并删除队列头数据,如队列为空则阻塞等待
                        System.out.println(alphabetBlockingQueue.take());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    numBlockingQueue.add(nums[i]);
                }
            }
        });
        t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < nums.length; i++) {
                    try {
                        System.out.println(numBlockingQueue.take());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (i < alphabets.length - 1) {
                        alphabetBlockingQueue.add(alphabets[i + 1]);
                    }
                }
            }
        });

        t1.start();
        t2.start();

        // 主线程阻塞等待子线程运行完毕
        System.in.read();
    }
}

方法4:SynchronousQueue实现

同步队列,起到和阻塞队列相似的作用。

	@Test
    public void alternatePrintV4() throws InterruptedException {
        final char[] nums = "1234567".toCharArray();
        final char[] alphabets = "ABCDEFG".toCharArray();
        SynchronousQueue<Character> numQueue = new SynchronousQueue<>();
        SynchronousQueue<Character> alphabetQueue = new SynchronousQueue<>();

        //打印字母
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < nums.length; i++) {
                    try {
                        System.out.println(alphabetQueue.take());
                        numQueue.put(nums[i]);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        });

        //打印数字
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < alphabets.length; i++) {
                    try {
                        alphabetQueue.put(alphabets[i]);
                        System.out.println(numQueue.take());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        t1.start();
        t2.start();

        Thread.sleep(10000);
    }

测试结果:

测试结果

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大树91

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

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

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

打赏作者

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

抵扣说明:

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

余额充值