N个线程交替输出的问题

1、两个不同的线程交替打印Kao、La。
2、两个线程,一个打印100内的奇数一个打印100内的偶数,交替执行。

N个线程交替输出:


    public static void main(String[] args) {
        //示例:5个线程交替输出100次
        Data data = new Data(100, 2);
        new Thread(new Print(data, 0, "Kao")).start();
        new Thread(new Print(data, 1, "La")).start();
        new Thread(new Print(data, 2, "")).start();
        new Thread(new Print(data, 3, "")).start();
        new Thread(new Print(data, 4, "")).start();

        //示例:2个线程交替输出Kao、La
//        Data data = new Data(100, 2);
//        new Thread(new Print(data, 0, "Kao")).start();
//        new Thread(new Print(data, 1, "La")).start();
    }



    //要打印的资源
    public static class Data {

        public Data(int total, int t) {
            this.total = total;
            this.t = t;
        }

        volatile int count;  //计数
        int total;  //交替输出n次
        int t;  //线程数
    }

    //打印奇数的线程
    public static class Print implements Runnable {
        private final int code; //编号
        private final Data data;  //资源
        private final String content;  //输出内容

        public Print(Data data, int code, String content) {
            this.data = data;
            this.code = code;
            this.content = content;
        }

        public void run() {
            while (data.count <= data.total) {
                synchronized (data) {
                    if (data.count % data.t == code) {
                        System.out.println(Thread.currentThread().getName() + " --- " + data.count + " --- " + content);
                        data.count++;
                        data.notifyAll();
                    } else {
                        try {
                            data.wait();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }


5个线程交替输出,结果:

Thread-0 --- 0 --- 
Thread-1 --- 1 --- 
Thread-0 --- 2 --- 
Thread-1 --- 3 --- 
Thread-0 --- 4 --- 
Thread-1 --- 5 --- 
Thread-0 --- 6 --- 
Thread-1 --- 7 --- 
Thread-0 --- 8 --- 
Thread-1 --- 9 --- 
Thread-0 --- 10 --- 
Thread-1 --- 11 --- 
Thread-0 --- 12 --- 
Thread-1 --- 13 --- 
Thread-0 --- 14 --- 
Thread-1 --- 15 --- 
Thread-0 --- 16 --- 
Thread-1 --- 17 --- 
Thread-0 --- 18 --- 
Thread-1 --- 19 --- 
Thread-0 --- 20 --- 
Thread-1 --- 21 --- 
Thread-0 --- 22 --- 
Thread-1 --- 23 --- 
Thread-0 --- 24 --- 
Thread-1 --- 25 --- 
Thread-0 --- 26 --- 
Thread-1 --- 27 --- 
Thread-0 --- 28 --- 
Thread-1 --- 29 --- 
Thread-0 --- 30 --- 
Thread-1 --- 31 --- 
Thread-0 --- 32 --- 
Thread-1 --- 33 --- 
Thread-0 --- 34 --- 
Thread-1 --- 35 --- 
Thread-0 --- 36 --- 
Thread-1 --- 37 --- 
Thread-0 --- 38 --- 
Thread-1 --- 39 --- 
Thread-0 --- 40 --- 
Thread-1 --- 41 --- 
Thread-0 --- 42 --- 
Thread-1 --- 43 --- 
Thread-0 --- 44 --- 
Thread-1 --- 45 --- 
Thread-0 --- 46 --- 
Thread-1 --- 47 --- 
Thread-0 --- 48 --- 
Thread-1 --- 49 --- 
Thread-0 --- 50 --- 
Thread-1 --- 51 --- 
Thread-0 --- 52 --- 
Thread-1 --- 53 --- 
Thread-0 --- 54 --- 
Thread-1 --- 55 --- 
Thread-0 --- 56 --- 
Thread-1 --- 57 --- 
Thread-0 --- 58 --- 
Thread-1 --- 59 --- 
Thread-0 --- 60 --- 
Thread-1 --- 61 --- 
Thread-0 --- 62 --- 
Thread-1 --- 63 --- 
Thread-0 --- 64 --- 
Thread-1 --- 65 --- 
Thread-0 --- 66 --- 
Thread-1 --- 67 --- 
Thread-0 --- 68 --- 
Thread-1 --- 69 --- 
Thread-0 --- 70 --- 
Thread-1 --- 71 --- 
Thread-0 --- 72 --- 
Thread-1 --- 73 --- 
Thread-0 --- 74 --- 
Thread-1 --- 75 --- 
Thread-0 --- 76 --- 
Thread-1 --- 77 --- 
Thread-0 --- 78 --- 
Thread-1 --- 79 --- 
Thread-0 --- 80 --- 
Thread-1 --- 81 --- 
Thread-0 --- 82 --- 
Thread-1 --- 83 --- 
Thread-0 --- 84 --- 
Thread-1 --- 85 --- 
Thread-0 --- 86 --- 
Thread-1 --- 87 --- 
Thread-0 --- 88 --- 
Thread-1 --- 89 --- 
Thread-0 --- 90 --- 
Thread-1 --- 91 --- 
Thread-0 --- 92 --- 
Thread-1 --- 93 --- 
Thread-0 --- 94 --- 
Thread-1 --- 95 --- 
Thread-0 --- 96 --- 
Thread-1 --- 97 --- 
Thread-0 --- 98 --- 
Thread-1 --- 99 --- 
Thread-0 --- 100 --- 

Process finished with exit code 0

2个线程交替输出Kao、La,结果:

Thread-0 --- 0 --- Kao
Thread-1 --- 1 --- La
Thread-0 --- 2 --- Kao
Thread-1 --- 3 --- La
Thread-0 --- 4 --- Kao
Thread-1 --- 5 --- La
Thread-0 --- 6 --- Kao
Thread-1 --- 7 --- La
Thread-0 --- 8 --- Kao
Thread-1 --- 9 --- La
Thread-0 --- 10 --- Kao
Thread-1 --- 11 --- La
Thread-0 --- 12 --- Kao
Thread-1 --- 13 --- La
Thread-0 --- 14 --- Kao
Thread-1 --- 15 --- La
Thread-0 --- 16 --- Kao
Thread-1 --- 17 --- La
Thread-0 --- 18 --- Kao
Thread-1 --- 19 --- La
Thread-0 --- 20 --- Kao
Thread-1 --- 21 --- La
Thread-0 --- 22 --- Kao
Thread-1 --- 23 --- La
Thread-0 --- 24 --- Kao
Thread-1 --- 25 --- La
Thread-0 --- 26 --- Kao
Thread-1 --- 27 --- La
Thread-0 --- 28 --- Kao
Thread-1 --- 29 --- La
Thread-0 --- 30 --- Kao
Thread-1 --- 31 --- La
Thread-0 --- 32 --- Kao
Thread-1 --- 33 --- La
Thread-0 --- 34 --- Kao
Thread-1 --- 35 --- La
Thread-0 --- 36 --- Kao
Thread-1 --- 37 --- La
Thread-0 --- 38 --- Kao
Thread-1 --- 39 --- La
Thread-0 --- 40 --- Kao
Thread-1 --- 41 --- La
Thread-0 --- 42 --- Kao
Thread-1 --- 43 --- La
Thread-0 --- 44 --- Kao
Thread-1 --- 45 --- La
Thread-0 --- 46 --- Kao
Thread-1 --- 47 --- La
Thread-0 --- 48 --- Kao
Thread-1 --- 49 --- La
Thread-0 --- 50 --- Kao
Thread-1 --- 51 --- La
Thread-0 --- 52 --- Kao
Thread-1 --- 53 --- La
Thread-0 --- 54 --- Kao
Thread-1 --- 55 --- La
Thread-0 --- 56 --- Kao
Thread-1 --- 57 --- La
Thread-0 --- 58 --- Kao
Thread-1 --- 59 --- La
Thread-0 --- 60 --- Kao
Thread-1 --- 61 --- La
Thread-0 --- 62 --- Kao
Thread-1 --- 63 --- La
Thread-0 --- 64 --- Kao
Thread-1 --- 65 --- La
Thread-0 --- 66 --- Kao
Thread-1 --- 67 --- La
Thread-0 --- 68 --- Kao
Thread-1 --- 69 --- La
Thread-0 --- 70 --- Kao
Thread-1 --- 71 --- La
Thread-0 --- 72 --- Kao
Thread-1 --- 73 --- La
Thread-0 --- 74 --- Kao
Thread-1 --- 75 --- La
Thread-0 --- 76 --- Kao
Thread-1 --- 77 --- La
Thread-0 --- 78 --- Kao
Thread-1 --- 79 --- La
Thread-0 --- 80 --- Kao
Thread-1 --- 81 --- La
Thread-0 --- 82 --- Kao
Thread-1 --- 83 --- La
Thread-0 --- 84 --- Kao
Thread-1 --- 85 --- La
Thread-0 --- 86 --- Kao
Thread-1 --- 87 --- La
Thread-0 --- 88 --- Kao
Thread-1 --- 89 --- La
Thread-0 --- 90 --- Kao
Thread-1 --- 91 --- La
Thread-0 --- 92 --- Kao
Thread-1 --- 93 --- La
Thread-0 --- 94 --- Kao
Thread-1 --- 95 --- La
Thread-0 --- 96 --- Kao
Thread-1 --- 97 --- La
Thread-0 --- 98 --- Kao
Thread-1 --- 99 --- La
Thread-0 --- 100 --- Kao

Process finished with exit code 0

扩展:
唤醒指定线程 https://www.cnblogs.com/myseries/p/12531986.html

你可能感兴趣的:(N个线程交替输出的问题)