📄 activitymanager.java
字号:
package prefuse.activity;
import java.util.ArrayList;
import prefuse.util.PrefuseConfig;
/**
* <p>The ActivityManager is responsible for scheduling and running timed
* activities that perform data processing and animation.</p>
*
* <p>The AcivityManager runs in its own separate thread of execution, and
* one instance is used to schedule activities from any number of currently
* active visualizations. The class is implemented as a singleton; the single
* instance of this class is interacted with through static methods. These
* methods are called by an Activity's run methods, and so are made only
* package visible here.</p>
*
* <p>Activity instances can be scheduled by using their
* {@link prefuse.activity.Activity#run()},
* {@link prefuse.activity.Activity#runAt(long)}, and
* {@link prefuse.activity.Activity#runAfter(Activity)}
* methods. These will automatically call the
* appropriate methods with the ActivityManager.</p>
*
* <p>For {@link prefuse.action.Action} instances, one can also register
* the actions with a {@link prefuse.Visualization} and use the
* visualizations provided run methods to launch Actions in a
* convenient fashion. The interface, which is backed by an {@link ActivityMap}
* instance, also provides a useful level of indirection, allowing actions
* to be changed dynamically without changes to code in other locations.
* </p>
*
* @author <a href="http://jheer.org">jeffrey heer</a>
* @see Activity
* @see prefuse.action.Action
*/
public class ActivityManager extends Thread {
private static ActivityManager s_instance;
private ArrayList m_activities;
private ArrayList m_tmp;
private long m_nextTime;
private boolean m_run;
/**
* Returns the active ActivityManager instance.
* @return the ActivityManager
*/
private synchronized static ActivityManager getInstance() {
if ( s_instance == null || !s_instance.isAlive() ) {
s_instance = new ActivityManager();
}
return s_instance;
}
/**
* Create a new ActivityManger.
*/
private ActivityManager() {
super("prefuse_ActivityManager");
m_activities = new ArrayList();
m_tmp = new ArrayList();
m_nextTime = Long.MAX_VALUE;
int priority = PrefuseConfig.getInt("activity.threadPriority");
if ( priority >= Thread.MIN_PRIORITY &&
priority <= Thread.MAX_PRIORITY )
{
this.setPriority(priority);
}
this.setDaemon(true);
this.start();
}
/**
* Stops the activity manager thread. All scheduled actvities are
* canceled, and then the thread is then notified to stop running.
*/
public static void stopThread() {
ActivityManager am;
synchronized ( ActivityManager.class ) {
am = s_instance;
}
if ( am != null )
am._stop();
}
/**
* Schedules an Activity with the manager.
* @param a the Activity to schedule
*/
static void schedule(Activity a) {
getInstance()._schedule(a, a.getStartTime());
}
/**
* Schedules an Activity to start immediately, overwriting the
* Activity's currently set startTime.
* @param a the Activity to schedule
*/
static void scheduleNow(Activity a) {
getInstance()._schedule(a, System.currentTimeMillis());
}
/**
* Schedules an Activity at the specified startTime, overwriting the
* Activity's currently set startTime.
* @param a the Activity to schedule
* @param startTime the time at which the activity should run
*/
static void scheduleAt(Activity a, long startTime) {
getInstance()._schedule(a, startTime);
}
/**
* Schedules an Activity to start immediately after another Activity.
* The second Activity will be scheduled to start immediately after the
* first one finishes, overwriting any previously set startTime. If the
* first Activity is cancelled, the second one will not run.
*
* This functionality is provided by using an ActivityListener to monitor
* the first Activity. The listener is removed upon completion or
* cancellation of the first Activity.
*
* This method does not effect the scheduling of the first Activity.
* @param before the first Activity to run
* @param after the Activity to run immediately after the first
*/
static void scheduleAfter(Activity before, Activity after) {
getInstance()._scheduleAfter(before, after);
}
/**
* Schedules an Activity to start immediately after another Activity.
* The second Activity will be scheduled to start immediately after the
* first one finishes, overwriting any previously set startTime. If the
* first Activity is cancelled, the second one will not run.
*
* This functionality is provided by using an ActivityListener to monitor
* the first Activity. The listener will persist across mulitple runs,
* meaning the second Activity will always be evoked upon a successful
* finish of the first.
*
* This method does not otherwise effect the scheduling of the first Activity.
* @param before the first Activity to run
* @param after the Activity to run immediately after the first
*/
static void alwaysScheduleAfter(Activity before, Activity after) {
getInstance()._alwaysScheduleAfter(before, after);
}
/**
* Cancels an Activity and removes it from this manager, called by
* an Activity when the activity needs to be cancelled.
* @param a The activity to cancel.
*/
static void cancelActivity(Activity a){
getInstance()._cancelActivity(a);
}
/**
* Returns the number of scheduled activities
* @return the number of scheduled activities
*/
public static int activityCount() {
return getInstance()._activityCount();
}
/**
* Stops the activity manager thread. All scheduled actvities are
* canceled, and then the thread is then notified to stop running.
*/
private synchronized void _stop() {
while ( m_activities.size() > 0 ) {
Activity a = (Activity)m_activities.get(m_activities.size()-1);
a.cancel();
}
_setRunning(false);
notify();
}
/**
* Schedules an Activity with the manager.
* @param a the Activity to schedule
*/
private void _schedule(Activity a, long startTime) {
if ( a.isScheduled() ) {
try { notifyAll(); } catch ( Exception e ) {}
return; // already scheduled, do nothing
}
a.setStartTime(startTime);
synchronized ( this ) {
m_activities.add(a);
a.setScheduled(true);
if ( startTime < m_nextTime ) {
m_nextTime = startTime;
notify();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -