Transition Detector : Utilities « Threads « Java

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. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Threads » UtilitiesScreenshots 
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
w___w_w.___java2_s.c__o__m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.