现在,我调查信号量。我在以下有关该主题的链接上进行了谷歌搜索:

link

该链接的作者撰写了有关使用信号量进行信号传递的文章。为了展示其工作原理,他编写了自定义信号量。

自定义信号量代码:

public class Semaphore {
  private boolean signal = false;

  public synchronized void take() {
    this.signal = true;
    this.notify();
  }

  public synchronized void release() throws InterruptedException{
    while(!this.signal) wait();
    this.signal = false;
  }

}

关于如何在代码中使用它,他写道:
public class SendingThread {
  Semaphore semaphore = null;

  public SendingThread(Semaphore semaphore){
    this.semaphore = semaphore;
  }

  public void run(){
    while(true){
      //do something, then signal
      this.semaphore.take();

    }
  }
}



public class RecevingThread {
  Semaphore semaphore = null;

  public ReceivingThread(Semaphore semaphore){
    this.semaphore = semaphore;
  }

  public void run(){
    while(true){
      this.semaphore.release();
      //receive signal, then do something...
    }
  }
}

主要的:
Semaphore semaphore = new Semaphore();

SendingThread sender = new SendingThread(semaphore);

ReceivingThread receiver = new ReceivingThread(semaphore);

receiver.start();
sender.start();

据我了解,执行顺序应遵循
send - receive
send - receive
send - receive
...

我尝试使用此蓝图编写自己的代码
public class SendReceiveWithCustomSemaphore {
    public static void main(String[] args) {
        MySemaphore mySemaphore = new MySemaphore();
        new Send(mySemaphore).start();
        new Receive(mySemaphore).start();
    }
}

class MySemaphore {
    boolean flag = false;

    public synchronized void take() throws InterruptedException {
        flag = true;
        notify();
    }

    public synchronized void release() throws InterruptedException {
        while (!flag) {
            wait();
        }
        flag = false;
    }
}

class Send extends Thread {
    MySemaphore mySemaphore;

    public Send(MySemaphore semaphore) {
        this.mySemaphore = semaphore;
    }

    @Override
    public void run() {
        int i = 0;
        while (i++ < 10) {
            System.out.println("send");
            try {
                mySemaphore.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Receive extends Thread {
    MySemaphore mySemaphore;

    public Receive(MySemaphore semaphore) {
        this.mySemaphore = semaphore;
    }

    @Override
    public void run() {
        while (true) {
            try {
                mySemaphore.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("receive");
        }
    }
}

输出:
send
send
send
send
send
send
send
send
send
send
receive

因此,这不是我的预期行为。

我犯了一个错误然后写了代码,或者我不理解概念?

作者想说什么?

最佳答案

查找更好的教程。

您看到的输出与我期望的差不多。 “发送者”线程永远不会阻塞,因此它将永远打印“发送”,“发送”,“发送”。同时,在“接收方”线程中,每次调用semaphore.release()方法时,它将被阻塞,直到发送方下次运行时为止。

我希望看到很多“发送”消息,偶尔会有“接收”消息混在一起-或多或少地描述了您看到的消息。

我不知道该示例应证明什么,但是对我而言,它给人的印象是作者不知道程序员如何期望信号量的行为。

一些作者提供了不执行操作的示例,或包含故意错误的示例,这些错误将在以后的示例中“修复”。您确定不遵循此类示例吗?

编辑:我点击了链接,看起来主要的问题是在take()和release()方法的定义中交换了名称。如果仅切换名称,则更有意义。

10-04 18:34
查看更多