A:a B:b C:c D:d E:e F:f G:g H:h I:i J:j K:k L:l M:m N:n O:o P:p Q:q R:r S:s T:t U:u V:v W:w X:x Y:y Z:z 1:1 2:2 3:3 4:4 5:5 6:6 7:7 8:8 9:9 10:10 11:11 12:12 13:13 14:14 15:15 16:16 17:17 18:18 19:19 20:20 21:21 22:22 23:23 24:24 25:25 26:26

以下是示例代码:

public class PrintThread implements Runnable {

    private char startChar;
    private int startNum;

    public PrintThread(char startChar, int startNum) {
        this.startChar = startChar;
        this.startNum = startNum;
    }

    @Override
    public void run() {
        for (int i = 0; i < 26; i++) {
            synchronized (PrintThread.class) {
                System.out.println(startNum + i + ":" + (char) (startChar + i));
                PrintThread.class.notifyAll();
                try {
                    PrintThread.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        synchronized (PrintThread.class) {
            PrintThread.class.notifyAll();
        }
    }

    public static void main(String[] args) {
        PrintThread aThread = new PrintThread('a', 1);
        PrintThread AThread = new PrintThread('A', 1);
        PrintThread numThread = new PrintThread('1', 1);

        new Thread(aThread).start();
        new Thread(AThread).start();
        new Thread(numThread).start();
    }
}

在该代码中,我们创建了一个PrintThread类,用于输出a~z、A~Z和1~26。每个线程都通过构造函数传入其起始字符和数字。在run()方法中,我们使用synchronized关键字同步代码块,确保线程安全并避免输出混乱。我们使用notifyAll()方法通知其他线程可以开始输出,然后让当前线程等待。等到其他线程输出完后,我们再次使用notifyAll()方法通知其他线程可以开始输出,当前线程也结束。最后,我们在main()方法中创建三个线程,并启动它们

用Java编写三个线程一个线程输出a~z一个线程输出A~Z一个线程输出1~26。要求输出格式如下:

原文地址: https://www.cveoy.top/t/topic/fI4p 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录