Java多线程交替打印数字和字母的实现方法

使用して回答:GPT-4o-mini によってJava Q&A Hub

質問:

Java面试题之多线程 数组A内容为 1,2,3,4...52 ,数组B内容为26个英文字母,使用两个线程分别输入两个数组,打印内容为:12a34b56c78e....... 这样的规律

回答:

要实现这个功能,我们可以使用Java的多线程机制。我们将创建两个线程:一个线程负责处理数组A(数字),另一个线程负责处理数组B(字母)。我们将使用wait()notify()方法来协调两个线程的执行顺序,以确保它们按照指定的规律交替打印内容。

以下是一个示例代码,展示了如何实现这个功能:

public class MultiThreadedPrint {
    private static final int SIZE_A = 52;
    private static final int SIZE_B = 26;
    private static final Object lock = new Object();
    private static int indexA = 0;
    private static int indexB = 0;
    private static boolean isAturn = true; // 用于控制哪个线程先执行

    public static void main(String[] args) {
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                int[] arrayA = new int[SIZE_A];
                for (int i = 0; i < SIZE_A; i++) {
                    arrayA[i] = i + 1; // 填充数组A
                }

                while (indexA < SIZE_A) {
                    synchronized (lock) {
                        while (!isAturn) {
                            try {
                                lock.wait(); // 等待B线程打印
                            } catch (InterruptedException e) {
                                Thread.currentThread().interrupt();
                            }
                        }

                        // 打印两个数字
                        if (indexA < SIZE_A) {
                            System.out.print(arrayA[indexA++]);
                        }
                        if (indexA < SIZE_A) {
                            System.out.print(arrayA[indexA++]);
                        }

                        isAturn = false; // 切换到B线程
                        lock.notify(); // 通知B线程
                    }
                }
            }
        });

        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                char[] arrayB = new char[SIZE_B];
                for (int i = 0; i < SIZE_B; i++) {
                    arrayB[i] = (char) ('a' + i); // 填充数组B
                }

                while (indexB < SIZE_B) {
                    synchronized (lock) {
                        while (isAturn) {
                            try {
                                lock.wait(); // 等待A线程打印
                            } catch (InterruptedException e) {
                                Thread.currentThread().interrupt();
                            }
                        }

                        // 打印一个字母
                        if (indexB < SIZE_B) {
                            System.out.print(arrayB[indexB++]);
                        }

                        isAturn = true; // 切换到A线程
                        lock.notify(); // 通知A线程
                    }
                }
            }
        });

        threadA.start();
        threadB.start();

        try {
            threadA.join();
            threadB.join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

代码说明:

  1. 数组初始化:我们创建了两个数组,arrayA包含1到52的数字,arrayB包含26个小写字母。
  2. 线程控制:使用一个lock对象来同步两个线程的执行。isAturn变量用于控制哪个线程应该执行。
  3. 打印逻辑
    • 线程A打印两个数字,然后通知线程B。
    • 线程B打印一个字母,然后通知线程A。
  4. 等待与通知:使用wait()notify()方法来实现线程间的协调。

运行结果:

运行该程序后,输出将会是按照要求的格式交替打印数字和字母,例如:12a34b56c78d...