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

两个线程交替打印A1B2C3 ... 的几种实现方式_第1张图片

有以下需求:
两个线程,需要打印字母和数字,格式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 numBlockingQueue = new ArrayBlockingQueue<>(1);
    private final BlockingQueue 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 numQueue = new SynchronousQueue<>();
        SynchronousQueue 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);
    }

测试结果:

两个线程交替打印A1B2C3 ... 的几种实现方式_第2张图片

你可能感兴趣的