Early return : Utilities « Threads « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Threads » UtilitiesScreenshots 
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(53000);
      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.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.