TransitionDetector.java
资源名称:Source.rar [点击查看]
上传用户:songled
上传日期:2022-07-14
资源大小:94k
文件大小:2k
源码类别:
进程与线程
开发平台:
Java
- public 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());
- }
- }