📄 taskmanager.java
字号:
//// The contents of this file are subject to the Mozilla Public// License Version 1.1 (the "License"); you may not use this file// except in compliance with the License. You may obtain a copy// of the License at http://www.mozilla.org/MPL/// // Software distributed under the License is distributed on an// "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or// implied. See the License for the specific language governing// rights and limitations under the License.// // The Original Code is State Machine Compiler (SMC).// // The Initial Developer of the Original Code is Charles W. Rapp.// Portions created by Charles W. Rapp are// Copyright (C) 2000 - 2003 Charles W. Rapp.// All Rights Reserved.// // Contributor(s): //// Name// TaskManager.java//// Description// This singleton is responsible for scheduling the running// task.//// RCS ID// $Id: TaskManager.java,v 1.6 2007/02/21 13:40:56 cwrapp Exp $//// CHANGE LOG// $Log: TaskManager.java,v $// Revision 1.6 2007/02/21 13:40:56 cwrapp// Moved Java code to release 1.5.0//// Revision 1.5 2005/05/28 13:51:24 cwrapp// Update Java examples 1 - 7.//// Revision 1.0 2003/12/14 20:14:18 charlesr// Initial revision//package smc_ex5;import javax.swing.Timer;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;public final class TaskManager implements TaskEventListener{// Member Methods. public TaskManager() { TaskController control = new TaskController(); _runningTask = null; _runnableTaskQueue = new LinkedList<Task>(); _blockedTaskList = new LinkedList<Task>(); _timerTable = new HashMap<String, Timer>(); _exitCode = 0; _fsm = new TaskManagerContext(this); // Uncomment to see debug output. // _fsm.setDebugFlag(true); // Register with the controller. control.register("Task Manager", this); } //=========================================================== // These methods respond to viewer messages. // // Create a new task. public void createTask(String name, int time, int priority) { if (name == null || name.length() == 0) { sendMessage(0, "Cannot create task without a name."); } else if (taskExists(name) == true) { sendMessage( 0, "Cannot create task named \"" + name + "\" - a task with that name already exists."); } else { Task newTask = new Task(name, priority, time); _runnableTaskQueue.add(newTask); sendMessage(1, "Created task " + name + "(priority: " + Integer.toString(priority) + ", time: " + Integer.toString(time) + ")."); _fsm.TaskCreated(); } return; } // Suspend the currently running task - if there is one. public void suspendTask() { if (_runningTask != null) { TaskController control = new TaskController(); sendMessage(2, "Suspending task " + _runningTask.getName() + "."); // Tell the task to suspend. control.postMessage(_runningTask.getName(), "suspend"); // Put the task back on to the runnable queue. _runnableTaskQueue.add(_runningTask); _runningTask = null; } return; } // Block the specified task. If that task is running, // then remove it. public void blockTask(String taskName) { Task task; if ((task = findTask(taskName)) != null) { TaskController control = new TaskController(); sendMessage(2, "Task " + taskName + " is blocked."); // Tell the task to block. control.postMessage(taskName, "block"); if (task == _runningTask) { _runningTask = null; } else { // Remove the task from the runnable queue. _runnableTaskQueue.remove(task); } _blockedTaskList.add(task); } return; } public void unblockTask(String taskName) { Task task; int taskIndex; // Is there a task with this name? if ((task = findTask(taskName)) != null) { // Is this task on the blocked list? taskIndex = _blockedTaskList.indexOf(task); if (taskIndex >= 0) { TaskController control = new TaskController(); sendMessage(2, "Task " + taskName + " is unblocked."); // Tell the task it is now unblocked. control.postMessage(task.getName(), "unblock"); // Move the task from the blocked queue to the // runnable queue. _blockedTaskList.remove(taskIndex); _runnableTaskQueue.add(task); _fsm.TaskUnblocked(); } } return; } public void deleteTask(String taskName) { Task task; if ((task = findTask(taskName)) != null) { TaskController control = new TaskController(); // Tell the task to go and die. control.postMessage(taskName, "delete"); } return; } // Shutting down the application. public void shutdown() { _fsm.Shutdown(); return; } //=========================================================== // These methods handle task object messages. // // The running task has completed its work. public void taskDone(String taskName) { Task task; int taskIndex; if ((task = findTask(taskName)) != null) { sendMessage(1, "Task " + taskName + " has completed."); // Is this the running task? if (task == _runningTask) { _runningTask = null; _fsm.TaskDone(); } else if ((taskIndex = _runnableTaskQueue.indexOf(task)) >= 0) { // I don't know how a suspended task managed to // complete. Remove it from a runnable list. _runnableTaskQueue.remove(taskIndex); } else if ((taskIndex = _blockedTaskList.indexOf(task)) >= 0) { // I don't know how a blocked task managed to // complete. Remove it from the blocked list. _blockedTaskList.remove(taskIndex); } } return; } // A task has stopped and is ready for deletion. public void taskStopped(String taskName) { Task task; int taskIndex; if ((task = findTask(taskName)) != null && (taskIndex = _blockedTaskList.indexOf(task)) >= 0) { sendMessage(1, "Task " + taskName + " is stopped."); _blockedTaskList.remove(taskIndex); _fsm.TaskStopped(); } else { sendMessage(4, "TaskManager::taskStopped: " + taskName + " not on blocked list."); } return; } // A task has stopped and is ready for deletion. public void taskDeleted(String taskName) { Task task; int taskIndex; if ((task = findTask(taskName)) != null) { sendMessage(1, "Task " + taskName + " deleted."); if (task == _runningTask) { _runningTask = null; _fsm.TaskDeleted(); } else if ( (taskIndex = _runnableTaskQueue.indexOf(task)) >= 0) { _runnableTaskQueue.remove(taskIndex); } else if ((taskIndex = _blockedTaskList.indexOf(task)) >= 0) { _blockedTaskList.remove(taskIndex); } } return; } //=========================================================== // State machine actions. // // Create a timer for the specified period. When the timer // expires, issue the corresponding state machine transition. public void setTimer(String name, int period) { Timer timer; // Is there a timer with this name already? if (_timerTable.containsKey(name) == true) { // Yes, there is. Stop the current timer and then // start it again. stopTimer(name); } timer = new Timer(period, new TimerListener(name, this)); timer.setRepeats(false); _timerTable.put(name, timer); // Start the timer running. timer.start(); return; } // Stop the named timer if it is running. public void stopTimer(String name) { Timer timer; // Remove the timer from the table and stop it. if ((timer = _timerTable.remove(name)) != null) { timer.stop(); } return; } // Send a message to the GUI controller so it can be posted // on the message display. public void sendMessage(int level, String message) { TaskController control = new TaskController();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -