KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > jsf > el > Waiter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.jsf.el;
21
22 /**
23  * A class taht may be used for waiting e.g. for a event.
24  * <p>
25  * For example:
26  * <p><code><pre>
27  * Observable obs;
28  * final Waiter waiter = new Waiter();
29  * final PropertyChangeListener pcl = new PropertyChangeListener() {
30  * public void propertyChange(PropertyChangeEvent evt) {
31  * if (evt.getPropertyName().equals("..")) {
32  * waiter.notifyFinished();
33  * }
34  * }
35  * };
36  * obs.addPropertyChangeListener(pcl);
37  * // ...
38  * waiter.waitFinished();
39  * obs.removePropertyChangeListener(pcl);
40  * </pre></code>
41  * <p>
42  *
43  * @author ms113234
44  */

45 public class Waiter {
46     
47     private boolean finished = false;
48     
49     /** Restarts Synchronizer.
50      */

51     public void init() {
52         synchronized (this) {
53             finished = false;
54         }
55     }
56     
57     /** Wait until the task is finished.
58      */

59     public void waitFinished() throws InterruptedException JavaDoc {
60         synchronized (this) {
61             while (!finished) {
62                 wait();
63             }
64         }
65     }
66     
67     /** Wait until the task is finished, but only a given time.
68      * @param milliseconds time in milliseconds to wait
69      * @return true if the task is really finished, or false if the time out
70      * has been exceeded
71      */

72     public boolean waitFinished(long milliseconds) throws InterruptedException JavaDoc {
73         synchronized (this) {
74             if (finished) return true;
75             long expectedEnd = System.currentTimeMillis() + milliseconds;
76             for (;;) {
77                 wait(milliseconds);
78                 if (finished) return true;
79                 long now = System.currentTimeMillis();
80                 if (now >= expectedEnd) return false;
81                 milliseconds = expectedEnd - now;
82             }
83         }
84     }
85     
86     /** Notify all waiters that this task has finished.
87      */

88     public void notifyFinished() {
89         synchronized (this) {
90             finished = true;
91             notifyAll();
92         }
93     }
94 }
95
Popular Tags