KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > app > util > Scheduler


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 package org.netbeans.test.editor.app.util;
20
21 import java.util.ArrayList JavaDoc;
22 import javax.swing.SwingUtilities JavaDoc;
23 /**
24  *
25  * @author jlahoda
26  * @version
27  */

28 public class Scheduler extends Thread JavaDoc {
29
30     private static Object JavaDoc synchronizeTo = new Object JavaDoc();
31     private static boolean allowedToCreate = true;
32     private ArrayList JavaDoc runnables; /*Of Runable's*/
33     private ArrayList JavaDoc run; /*Of Runable's*/
34     private static Scheduler scheduler = null;
35     private boolean shouldFinish = false;
36
37     private static boolean superSafe = false;
38     private static boolean superSafeChanged = false;
39     
40     public static boolean getSuperSafe() {
41         return superSafe;
42     }
43     
44     public static void setSuperSafe(boolean newState) {
45         if (superSafeChanged)
46             return;
47         superSafe = newState;
48         superSafeChanged = true;
49     }
50     
51     public static synchronized Scheduler getDefault() {
52         if (scheduler == null /*&& allowedToCreate*/) {
53             new Scheduler();
54             scheduler.start();
55         }
56         return scheduler;
57     }
58     
59     public static void finishScheduler() {
60         if (scheduler == null) {
61 /* synchronized (synchronizeTo) {
62                 if (scheduler == null) {
63                     allowedToCreate = false;
64                 }
65             }*/

66         } else {
67             getDefault().finish();
68         }
69     }
70     
71     /** Creates new Scheduler */
72     protected Scheduler() {
73         runnables = new ArrayList JavaDoc();
74         run = new ArrayList JavaDoc();
75         scheduler = this;
76     }
77     
78     public void addTask(Runnable JavaDoc what) {
79         synchronized (runnables) {
80             runnables.add(what);
81         }
82     }
83     
84     public void finish() {
85         shouldFinish = true;
86         // allowedToCreate = false;
87
}
88     
89     public void run() {
90         boolean finish = false;
91         
92         while (!finish) {
93             while (!shouldFinish || (runnables.size() > 0)) {
94                 if (runnables.size() > 0) {
95                     Runnable JavaDoc toRun = null;
96                     synchronized (runnables) {
97                         toRun = (Runnable JavaDoc)runnables.get(0);
98                         runnables.remove(0);
99                     }
100                     if (superSafe) {
101                         SwingUtilities.invokeLater(toRun);
102                     } else {
103                         try {
104                             SwingUtilities.invokeAndWait(toRun);
105                         } catch (java.lang.InterruptedException JavaDoc e) {
106                             System.err.println("Unexpected interrupt of invokeAndWait(): " + e);
107                             e.printStackTrace(System.err);
108                         } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
109                             System.err.println("Unexpected exception in invokeAndWait: " + e);
110                             e.printStackTrace(System.err);
111                         }
112                     }
113                     try {
114                         sleep(10);
115                     } catch (InterruptedException JavaDoc e) {
116                         System.err.println("Interrupted.");
117                     }
118                 } else {
119                     //yield();
120
try {
121                     sleep(200);
122                     } catch (Exception JavaDoc ex) {
123                     }
124                 }
125             }
126             synchronized (synchronizeTo) {
127                 if (runnables.size() > 0) {
128                     finish = false;
129                 } else {
130                     finish = true;
131                     scheduler = null;
132                 }
133             }
134         }
135     }
136 }
137
Popular Tags