Skip to content

@Signal @Await @SignalBeforeAwaitAfter

framiere edited this page Nov 24, 2011 · 2 revisions

@Signal/@Await/@SignalBeforeAwaitAfter

- WORK IN PROGRESS - (Initial copy of old documentation)

Overview

Usage

With Lombok

import lombok.AccessLevel;
import lombok.Await;
import lombok.Getter;
import lombok.Signal;

class ConditionExample {
  @Getter(AccessLevel.PRIVATE)
  private volatile boolean paused;
  
  @Signal("canResume")
  void unpause() {
    paused = false;
  }
  
  @Await(conditionName = "canResume", conditionMethod="isPaused")
  void pause() {
  }
}

Vanilla Java

class ConditionExample {
  private volatile boolean paused;
  private final java.util.concurrent.locks.Lock $canResumeLock = new java.util.concurrent.locks.ReentrantLock();
  private final java.util.concurrent.locks.Condition canResume = $canResumeLock.newCondition();

  void unpause() {
    this.$canResumeLock.lock();
    try {
      paused = false;
      this.canResume.signal();
    } finally {
      this.$canResumeLock.unlock();
    }
  }

  void pause() {
    this.$canResumeLock.lock();
    try {
      try {
        while (this.isPaused()) {
          this.canResume.await();
        }
      } catch (final java.lang.InterruptedException e) {
        throw new RuntimeException(e);
      }
    } finally {
      this.$canResumeLock.unlock();
    }
  }

  private boolean isPaused() {
    return this.paused;
  }
}