代码之家  ›  专栏  ›  技术社区  ›  Orca

Java等价于.NET的ManualResetEvent和WaitHandle

  •  12
  • Orca  · 技术社区  · 14 年前

    WaitHandle和ManualResetEvent的.NET类提供了一个很好的、无麻烦的接口,据我所知,这个接口也是线程安全的,那么Java必须提供什么呢?

    4 回复  |  直到 13 年前
        1
  •  14
  •   Jon Skeet    14 年前

    你考虑过使用 wait / notify (相当于 Monitor.Wait Monitor.Pulse )相反呢?

    需要 等待(以避免竞赛条件),但它应该起作用。

    否则,就像 CountDownLatch 你想干什么就干什么。

    Semaphore tryAcquire 像这样等待超时:

    if (semaphore.tryAquire(5, TimeUnit.SECONDS)) {
       ...
       // Permit was granted before timeout
    } else {
       // We timed out while waiting
    }
    

    请注意,这与 ManualResetEvent 每一次成功的呼叫 尝试获取 会减少许可证的数量-所以最终他们会再次用完。你不能让它永久“设置”就像你可以与 手动复位事件 . (这将与 CountdownLatch ,但您无法“重置”它:)

        2
  •  4
  •   terentev    13 年前
    class ManualResetEvent {
    
      private final Object monitor = new Object();
      private volatile boolean open = false;
    
      public ManualResetEvent(boolean open) {
        this.open = open;
      }
    
      public void waitOne() throws InterruptedException {
        synchronized (monitor) {
          while (open==false) {
              monitor.wait();
          }
        }
      }
    
      public void set() {//open start
        synchronized (monitor) {
          open = true;
          monitor.notifyAll();
        }
      }
    
      public void reset() {//close stop
        open = false;
      }
    }
    
        3
  •  3
  •   Preet Sangha    14 年前

    发件人: http://www.experts-exchange.com/Programming/Languages/Java/Q_22076798.html

    嗨,您可以使用java.util.concurrent文件.Semaphore类(使用0 permit)。

    http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Semaphore.html

    下面的示例向您展示了如何解决第一个同步问题,另一个类似:

    import java.util.concurrent.Semaphore;
    
    class ScalesCommunication {
    
       private static Semaphore sem = new Semaphore(0);
    
       // called by thread 1
       void readLoop() {
          //...
    
          //after connection established, release semaphore (value incremented by 1)
          sem.release();
       }
    
       // called by thread 2
       String sendCommand(String command) {
    
           sem.acquire(); // thread waits here if sem value == 0
    
           // at this point connection is established
           //...
       }
    }
    
        4
  •  1
  •   Robin Davies    12 年前

       class ManualResetEvent {
          private final Object monitor = new Object();
          private volatile boolean open = false;
    
          public ManualResetEvent(boolean open) {
            this.open = open;   }
    
          public void waitOne() throws InterruptedException {
            synchronized (monitor) {
              while (open==false) {
                  monitor.wait();
              }
            }
          }
    
          public void set() {//open start
            synchronized (monitor) {
              open = true;
              monitor.notifyAll();
            }
          }
    
          public void reset() {//close stop
            synchronized(monitor) {
               open = false;
            }
          }
       }
    

    多亏了原来的海报。