Transition Detector : 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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Threads » Utilities 




Transition Detector
Transition Detector
  
public class TransitionDetectorMain extends Object {
  private static Thread startTrueWaiter(final TransitionDetector td,
      String name) {

    Runnable r = new Runnable() {
      public void run() {
        try {
          while (true) {
            print("about to wait for false-to-"
                "true transition, td=" + td);

            td.waitForFalseToTrueTransition();

            print("just noticed for false-to-"
                "true transition, td=" + td);
          }
        catch (InterruptedException ix) {
          return;
        }
      }
    };

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

    return t;
  }

  private static Thread startFalseWaiter(final TransitionDetector td,
      String name) {

    Runnable r = new Runnable() {
      public void run() {
        try {
          while (true) {
            print("about to wait for true-to-"
                "false transition, td=" + td);

            td.waitForTrueToFalseTransition();

            print("just noticed for true-to-"
                "false transition, td=" + td);
          }
        catch (InterruptedException ix) {
          return;
        }
      }
    };

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

    return t;
  }

  private static void print(String msg) {
    String name = Thread.currentThread().getName();
    System.err.println(name + ": " + msg);
  }

  public static void main(String[] args) {
    try {
      TransitionDetector td = new TransitionDetector(false);

      Thread threadA = startTrueWaiter(td, "threadA");
      Thread threadB = startFalseWaiter(td, "threadB");

      Thread.sleep(200);
      print("td=" + td + ", about to set to 'false'");
      td.setValue(false);

      Thread.sleep(200);
      print("td=" + td + ", about to set to 'true'");
      td.setValue(true);

      Thread.sleep(200);
      print("td=" + td + ", about to pulse value");
      td.pulseValue();

      Thread.sleep(200);
      threadA.interrupt();
      threadB.interrupt();
    catch (InterruptedException x) {
      x.printStackTrace();
    }
  }
}

class TransitionDetector extends Object {
  private boolean value;

  private Object valueLock;

  private Object falseToTrueLock;

  private Object trueToFalseLock;

  public TransitionDetector(boolean initialValue) {
    value = initialValue;
    valueLock = new Object();
    falseToTrueLock = new Object();
    trueToFalseLock = new Object();
  }

  public void setValue(boolean newValue) {
    synchronized (valueLock) {
      if (newValue != value) {
        value = newValue;

        if (value) {
          notifyFalseToTrueWaiters();
        else {
          notifyTrueToFalseWaiters();
        }
      }
    }
  }

  public void pulseValue() {
    // Sync on valueLock to be sure that no other threads
    // get into setValue() between these two setValue()
    // calls.
    synchronized (valueLock) {
      setValue(!value);
      setValue(!value);
    }
  }

  public boolean isTrue() {
    synchronized (valueLock) {
      return value;
    }
  }

  public void waitForFalseToTrueTransition() throws InterruptedException {

    synchronized (falseToTrueLock) {
      falseToTrueLock.wait();
    }
  }

  private void notifyFalseToTrueWaiters() {
    synchronized (falseToTrueLock) {
      falseToTrueLock.notifyAll();
    }
  }

  public void waitForTrueToFalseTransition() throws InterruptedException {

    synchronized (trueToFalseLock) {
      trueToFalseLock.wait();
    }
  }

  private void notifyTrueToFalseWaiters() {
    synchronized (trueToFalseLock) {
      trueToFalseLock.notifyAll();
    }
  }

  public String toString() {
    return String.valueOf(isTrue());
  }
}



           
         
    
  














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.Early returnEarly return
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.