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

问题描述 投票:0回答:1

我已经学习了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...");
        }
    }
}

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

java multithreading semaphore
1个回答
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,即继续进行,[[每秒迭代。
© www.soinside.com 2019 - 2024. All rights reserved.