多线程(3个线程)输出整数序列 010203040506... ,其中序列的长度必须为 2n。

打印零与奇偶数
假设有这么一个类:

class ZeroEvenOdd {
public ZeroEvenOdd(int n) { ... }  // 构造函数
public void zero(printNumber) { ... } // 仅打印出 0
public void even(printNumber) { ... } // 仅打印出 偶数
public void odd(printNumber) { ... } // 仅打印出 奇数
}
相同的一个 ZeroEvenOdd 类实例将会传递给三个不同的线程:

线程 A 将调用 zero(),它只输出 0 。
线程 B 将调用 even(),它只输出偶数。
线程 C 将调用 odd(),它只输出奇数。
每个线程都有一个 printNumber 方法来输出一个整数。请修改给出的代码以输出整数序列 010203040506... ,其中序列的长度必须为 2n。

package com.ctl.algorithm;


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

/**
 * <p>Title: ZeroEvenOddTest</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2019</p>
 * <p>Company: www.hanshow.com</p>
 *
 * @author ctl
 * @version 1.0
 * @date 2021-09-28 22:35
 */
public class ZeroEvenOdd2Test {
    static class ZeroEvenOdd {
        int n;

        public ZeroEvenOdd(int n) {
            this.n = n;
        }  // 构造函数

        public void zero(int printNumber) {
            System.out.print(printNumber);
        } // 仅打印出 0

        public void even(int printNumber) {
            System.out.print(printNumber);
        } // 仅打印出 偶数

        public void odd(int printNumber) {
            System.out.print(printNumber);
        } // 仅打印出 奇数
    }

    private static Semaphore zero = new Semaphore(0);
    private static Semaphore odd = new Semaphore(0);
    private static Semaphore even = new Semaphore(0);
    private static CountDownLatch countDownLatch = new CountDownLatch(3);


    public static void main(String[] args) throws Exception {
        int n = 3;
        ZeroEvenOdd zeroEvenOdd = new ZeroEvenOdd(n);
        new Thread(() -> {
            for (int i = 0; i < n; i++) {
                try {
                    zero.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                zeroEvenOdd.zero(0);
                if (i % 2 == 0) {
                    odd.release();
                } else {
                    even.release();
                }
            }
            countDownLatch.countDown();

        }).start();
        new Thread(() -> {
            for (int i = 1; i <= n; i = i + 2) {
                try {
                    odd.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                zeroEvenOdd.odd(i);
                zero.release();
            }
            countDownLatch.countDown();

        }).start();
        new Thread(() -> {
            for (int i = 2; i <= n; i = i + 2) {
                try {
                    even.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                zeroEvenOdd.even(i);
                zero.release();
            }
            countDownLatch.countDown();

        }).start();
        zero.release();
        countDownLatch.await();
        System.out.println("\nover");
    }
}

Guess you like

Origin blog.csdn.net/CTLLIN/article/details/120550844