簡體   English   中英

在線程中運行的信號量任務

[英]Semaphores task running in threads

我已經關注 Java class。 第一個任務是修改類以獲得ABCABCABCABC序列,使用非常簡單的線程。 I simply did it by putting a.acquire b.realse inside A class, b.acquire c.release inside 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乘以 2,因為循環必須運行兩倍,因為B字母被打印兩次。
  • i % 2條件允許釋放信號量C ,即每第二次迭代進行一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM