Early return : Utilities « Threads « Java






Early return

Early return
  
public class EarlyReturn extends Object {
  private volatile int value;

  public EarlyReturn(int v) {
    value = v;
  }

  public synchronized void setValue(int v) {
    if (value != v) {
      value = v;
      notifyAll();
    }
  }

  public synchronized boolean waitUntilAtLeast(int minValue, long msTimeout)
      throws InterruptedException {

    System.out.println("value=" + value + ",minValue=" + minValue);

    long endTime = System.currentTimeMillis() + msTimeout;
    long msRemaining = msTimeout;

    while ((value < minValue) && (msRemaining > 0L)) {
      System.out.println("about to: wait(" + msRemaining + ")");
      wait(msRemaining);
      msRemaining = endTime - System.currentTimeMillis();
      System.out.println("back from wait(), new msRemaining="
          + msRemaining);
    }

    System.out.println("leaving waitUntilAtLeast() - " + "value=" + value
        + ",minValue=" + minValue);

    // May have timed out, or may have met value,
    return (value >= minValue);
  }

  public static void main(String[] args) {
    try {
      final EarlyReturn er = new EarlyReturn(0);

      Runnable r = new Runnable() {
        public void run() {
          try {
            Thread.sleep(1500);
            er.setValue(2);
            Thread.sleep(500);
            er.setValue(3);
            Thread.sleep(500);
            er.setValue(4);
          } catch (Exception x) {
            x.printStackTrace();
          }
        }
      };

      Thread t = new Thread(r);
      t.start();

      System.out.println("waitUntilAtLeast(5, 3000)");
      long startTime = System.currentTimeMillis();
      boolean retVal = er.waitUntilAtLeast(5, 3000);
      long elapsedTime = System.currentTimeMillis() - startTime;

      System.out.println(elapsedTime + " ms, retVal=" + retVal);
    } catch (InterruptedException ix) {
      ix.printStackTrace();
    }
  }
}

           
         
    
  








Related examples in the same category

1.View current Threads in a table View current Threads in a table
2.Exception call backException call back
3.Listing all threads and threadgroups in the VM.Listing all threads and threadgroups in the VM.
4.Transition DetectorTransition Detector
5.Busy Flag
6.Sleep utilities
7.Thread-based logging utility
8.Return a new instance of the given class. Checks the ThreadContext classloader first, then uses the System classloader.
9.Finds a resource with the given name.