欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java 实现的信号量计数,java信号量计数,Counting Sem

来源: javaer 分享于  点击 27681 次 点评:279

Java 实现的信号量计数,java信号量计数,Counting Sem


Counting Semaphore

package com.javapapers.thread;public class CountingSemaphore {  private int value = 0;  private int waitCount = 0;  private int notifyCount = 0;  public CountingSemaphore(int initial) {    if (initial > 0) {      value = initial;    }  }  public synchronized void waitForNotify() {    if (value <= waitCount) {      waitCount++;      try {        do {          wait();        } while (notifyCount == 0);      } catch (InterruptedException e) {        notify();      } finally {        waitCount--;      }      notifyCount--;    }    value--;  }  public synchronized void notifyToWakeup() {    value++;    if (waitCount > notifyCount) {      notifyCount++;      notify();    }  }}

Binary Semaphore

package com.javapapers.thread;public class BinarySemaphore {  private boolean locked = false;  BinarySemaphore(int initial) {    locked = (initial == 0);  }  public synchronized void waitForNotify() throws InterruptedException {    while (locked) {      wait();    }    locked = true;  }  public synchronized void notifyToWakeup() {    if (locked) {      notify();    }    locked = false;  }}

测试类 SemaphoreABC.java

package com.javapapers.thread;import java.util.Random;public class SemaphoreABC {  protected static final BinarySemaphore binarySemaphore0 = new BinarySemaphore(      0);  protected static final BinarySemaphore binarySemaphore1 = new BinarySemaphore(      1);  protected static final CountingSemaphore countingSemaphore = new CountingSemaphore(      0);  protected static final Random random = new Random();  public static void main(String args[]) throws InterruptedException {    new Thread(new ProcessA()).start();    new Thread(new ProcessB()).start();    new Thread(new ProcessC()).start();    Thread.sleep(9000);    System.exit(0);  }}

ProcessA.java

package com.javapapers.thread;public class ProcessA extends SemaphoreABC implements Runnable {  public void run() {    while (true) {      try {        Thread.sleep(1 + (int) (random.nextDouble() * 500));      } catch (InterruptedException e1) {        e1.printStackTrace();      }      System.out.print("A");      countingSemaphore.notifyToWakeup();    }  }}

ProcessB.java

package com.javapapers.thread;public class ProcessB extends SemaphoreABC implements Runnable {  public void run() {    while (true) {      try {        Thread.sleep(1 + (int) (random.nextDouble() * 800));        binarySemaphore1.waitForNotify();      } catch (InterruptedException e) {        e.printStackTrace();      }      countingSemaphore.waitForNotify();      System.out.print("B");      binarySemaphore0.notifyToWakeup();    }  }}

ProcessC.java

package com.javapapers.thread;public class ProcessC extends SemaphoreABC implements Runnable {  public void run() {    while (true) {      try {        Thread.sleep(1 + (int) (random.nextDouble() * 800));        binarySemaphore0.waitForNotify();      } catch (InterruptedException e) {        e.printStackTrace();      }      countingSemaphore.waitForNotify();      System.out.print("C");      binarySemaphore1.notifyToWakeup();    }  }}

输出

AABCABACAABCAABCAAABCABCABACAABCABACABACABACABAC
相关栏目:

用户点评