在线程中运行的信号量任务

时间:2019-11-15 15:52:40

标签: java multithreading semaphore

我有下面的Java课。第一个任务是使用非常简单的线程来修改类以获得ABCABCABCABC序列。我只是通过在A类中放入a.acquire b.realse,在C类中放入b.acquire c.release以及c.acquire和a.relase来做到这一点。

现在,我必须修改类以获取 ABBCABBC序列,但是我为此感到挣扎。有人知道如何处理吗?

类代码:

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...");
        }
    }
}

此外,对于为什么解决方案应该像您的解决方案的一些解释也会派上用场。预先谢谢你。

1 个答案:

答案 0 :(得分:0)

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被乘以2,因为循环必须运行两倍,因为B字母被打印两次。
  • i % 2条件允许释放信号量C,即继续进行每秒迭代。