线程中的信号量任务 运行

Semaphores task running in threads

我有关注 Java class。第一个任务是修改 classes 以获得 ABCABCABCABC 序列,使用非常简单的线程。我只是通过将 a.acquire b.realse 放入 A class、b.acquire c.release 放入 C class and c.acquire and a.relase.

现在,我必须修改 class 以获得 ABBCABBC 序列,但我正在努力解决这个问题。有人知道如何处理吗?

Class代码:

import java.util.concurrent.Semaphore;

public class SemaphoresABC {

    private static final int COUNT = 10; //Number of letters displayed by threads
    private static final int DELAY = 5; //delay, in milliseconds, used to put a thread to sleep

    private static final Semaphore a = new Semaphore(1, true);
    private static final Semaphore b = new Semaphore(0, true);
    private static final Semaphore c = new Semaphore(0, true);

    public static void main(String[] args) {
        new A().start(); //runs a thread defined below 
        new B().start();
        new C().start();

    }

    private static final class A extends Thread { //thread definition

        @Override
        @SuppressWarnings("SleepWhileInLoop")
        public void run() {
            try {
                for (int i = 0; i < COUNT; i++) {
                    //use semaphores here

                    System.out.print("A ");
                    //use semaphores here

                    Thread.sleep(DELAY);
                }
            } catch (InterruptedException ex) {
                System.out.println("Ooops...");
                Thread.currentThread().interrupt();
                throw new RuntimeException(ex);
            }
            System.out.println("\nThread A: I'm done...");
        }
    }

    private static final class B extends Thread {

        @Override
        @SuppressWarnings("SleepWhileInLoop")
        public void run() {
            try {
                for (int i = 0; i < COUNT; i++) {
                    //use semaphores here

                    System.out.print("B ");
                    //use semaphores here

                    Thread.sleep(DELAY);
                }
            } catch (InterruptedException ex) {
                System.out.println("Ooops...");
                Thread.currentThread().interrupt();
                throw new RuntimeException(ex);
            }
            System.out.println("\nThread B: I'm done...");
        }
    }

    private static final class C extends Thread {

        @Override
        @SuppressWarnings("SleepWhileInLoop")
        public void run() {
            try {
                for (int i = 0; i < COUNT; i++) {
                    //use semaphores here

                    System.out.print("C ");
                    //use semaphores here

                    Thread.sleep(DELAY);
                }
            } catch (InterruptedException ex) {
                System.out.println("Ooops...");
                Thread.currentThread().interrupt();
                throw new RuntimeException(ex);
            }
            System.out.println("\nThread C: I'm done...");
        }
    }
}

另外,一些解释为什么解决方案应该像你的那样,会派上用场。提前谢谢你。

循环 B 字母应修改如下:

for (int i = 0; i < COUNT * 2; i++) {

    b.acquire();

    System.out.print("B ");

    if (i % 2 == 1) {
        c.release();
    } else {
        b.release();
    }

    Thread.sleep(DELAY);
}    

这里:

  • COUNT 乘以二,因为循环必须 运行 两倍,因为 B 字母被打印两次。
  • i % 2 条件允许释放信号量 C,即继续,每秒 迭代。