1 package prefuse.activity; 2 3 import java.util.ArrayList ; 4 5 import prefuse.util.PrefuseConfig; 6 7 8 38 public class ActivityManager extends Thread { 39 40 private static ActivityManager s_instance; 41 42 private ArrayList m_activities; 43 private ArrayList m_tmp; 44 private long m_nextTime; 45 private boolean m_run; 46 47 51 private synchronized static ActivityManager getInstance() { 52 if ( s_instance == null || !s_instance.isAlive() ) { 53 s_instance = new ActivityManager(); 54 } 55 return s_instance; 56 } 57 58 61 private ActivityManager() { 62 super("prefuse_ActivityManager"); 63 m_activities = new ArrayList (); 64 m_tmp = new ArrayList (); 65 m_nextTime = Long.MAX_VALUE; 66 67 int priority = PrefuseConfig.getInt("activity.threadPriority"); 68 if ( priority >= Thread.MIN_PRIORITY && 69 priority <= Thread.MAX_PRIORITY ) 70 { 71 this.setPriority(priority); 72 } 73 this.setDaemon(true); 74 this.start(); 75 } 76 77 81 public static void stopThread() { 82 ActivityManager am; 83 synchronized ( ActivityManager.class ) { 84 am = s_instance; 85 } 86 if ( am != null ) 87 am._stop(); 88 } 89 90 94 static void schedule(Activity a) { 95 getInstance()._schedule(a, a.getStartTime()); 96 } 97 98 103 static void scheduleNow(Activity a) { 104 getInstance()._schedule(a, System.currentTimeMillis()); 105 } 106 107 113 static void scheduleAt(Activity a, long startTime) { 114 getInstance()._schedule(a, startTime); 115 } 116 117 131 static void scheduleAfter(Activity before, Activity after) { 132 getInstance()._scheduleAfter(before, after); 133 } 134 135 150 static void alwaysScheduleAfter(Activity before, Activity after) { 151 getInstance()._alwaysScheduleAfter(before, after); 152 } 153 154 163 static void removeActivity(Activity a) { 164 getInstance()._removeActivity(a); 165 } 166 167 171 public static int activityCount() { 172 return getInstance()._activityCount(); 173 } 174 175 179 private synchronized void _stop() { 180 while ( m_activities.size() > 0 ) { 181 Activity a = (Activity)m_activities.get(m_activities.size()-1); 182 a.cancel(); 183 } 184 _setRunning(false); 185 notify(); 186 } 187 188 192 private void _schedule(Activity a, long startTime) { 193 if ( a.isScheduled() ) { 194 return; } 196 a.setStartTime(startTime); 197 synchronized ( this ) { 198 m_activities.add(a); 199 a.setScheduled(true); 200 if ( startTime < m_nextTime ) { 201 m_nextTime = startTime; 202 notify(); 203 } 204 } 205 } 206 207 221 private void _scheduleAfter(Activity before, Activity after) { 222 before.addActivityListener(new ScheduleAfterActivity(after,true)); 223 } 224 225 240 private void _alwaysScheduleAfter(Activity before, Activity after) { 241 before.addActivityListener(new ScheduleAfterActivity(after,false)); 242 } 243 244 253 private boolean _removeActivity(Activity a) { 254 boolean r; 255 synchronized ( this ) { 256 r = m_activities.remove(a); 257 if ( r ) { 258 if ( m_activities.size() == 0 ) { 259 m_nextTime = Long.MAX_VALUE; 260 } 261 } 262 } 263 if ( r ) { 264 a.setScheduled(false); 265 } 266 return r; 267 } 268 269 273 private synchronized int _activityCount() { 274 return m_activities.size(); 275 } 276 277 280 private synchronized void _setRunning(boolean b) { 281 m_run = b; 282 } 283 284 288 private synchronized boolean _keepRunning() { 289 return m_run; 290 } 291 292 296 public void run() { 297 _setRunning(true); 298 while ( _keepRunning() ) { 299 if ( _activityCount() > 0 ) { 300 long currentTime = System.currentTimeMillis(); 301 long t = -1; 302 303 synchronized (this) { 304 for ( int i=0; i<m_activities.size(); i++ ) { 307 Activity a = (Activity)m_activities.get(i); 308 m_tmp.add(a); 309 310 if ( currentTime >= a.getStopTime() ) 312 { 313 m_activities.remove(i--); 314 a.setScheduled(false); 315 } 316 } 317 if ( m_activities.size() == 0 ) { 319 m_nextTime = Long.MAX_VALUE; 320 } 321 } 322 323 for ( int i=0; i<m_tmp.size(); i++ ) { 324 Activity a = (Activity)m_tmp.get(i); 327 long s = a.runActivity(currentTime); 328 t = (s<0 ? t : t<0 ? s : Math.min(t,s)); 330 } 331 332 m_tmp.clear(); 334 335 if ( t == -1 ) continue; 336
|