⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 taskmanager.java

📁 SMC takes a state machine stored in a .sm file and generates a State pattern in twelve programming l
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        Map<String, Object> args = new HashMap<String, Object>();        args.put("level", new Integer(level));        args.put("object", "TaskManager");        args.put("message", message);        control.postMessage("Message GUI",                            "Post Message",                            args);        return;    }    // Check if there are any tasks to run. If yes, then    // asynchronously issue a RunTask transition using the    // setTimer() method.    public void checkTaskQueue()    {        if (_runnableTaskQueue.size() > 0)        {            // Create a timer which will expire immediately.            setTimer("Run Task", 0);        }        return;    }    // Return the currently running task.    public Task getRunningTask()    {        return (_runningTask);    }    // Return the number of runnable tasks.    public int getRunnableTaskCount()    {        return (_runnableTaskQueue.size());    }    // Return the number of blocked tasks.    public int getBlockedTaskCount()    {        return (_blockedTaskList.size());    }    // Check if there are any tasks to run.    public boolean areTasksQueued()    {        return (_runnableTaskQueue.size() > 0);    }    // Task the highest priority task off the runnable queue    // and have it start running.    public void startTask()    {        Iterator<Task> taskIt;        Task task;        int index;        int taskIndex;        int taskPriority;        int currentMinPriority;        // Find the task with the lowest priority.        for (taskIt = _runnableTaskQueue.iterator(),                     currentMinPriority = Integer.MAX_VALUE,                     index = 0,                     taskIndex = -1;             taskIt.hasNext() == true;             ++index)        {            task = taskIt.next();            taskPriority = task.getDynamicPriority();            // Is the new task's priority less than            // the current task.             if (taskPriority < currentMinPriority)            {                taskIndex = index;                currentMinPriority = taskPriority;            }        }        // Was a task found?        if (taskIndex >= 0)        {            TaskController control = new TaskController();            _runningTask =                    (Task) _runnableTaskQueue.remove(taskIndex);             sendMessage(2,                         "Attempting to run task " +                         _runningTask.getName() +                         ".");             control.postMessage(_runningTask.getName(),                                 "start");        }        return;    }    // Cancel all existing timers.    public void stopAllTimers()    {        for (Timer timer: _timerTable.values())        {            timer.stop();        }        _timerTable.clear();        return;    }    public void stopAllTasks()    {        TaskController control = new TaskController();        // Put all tasks into the blocked list. As they report        // that they are stopped, remove the tasks.        //        // Do the blocked list first.        for (Task task: _blockedTaskList)        {            sendMessage(3,                        "Stopping task " +                        task.getName() +                        ".");            control.postMessage(task.getName(), "stop");        }        // Do the runnable tasks next.        for (Task task: _runnableTaskQueue)        {            sendMessage(3,                        "Stopping task " +                        task.getName() +                        ".");            control.postMessage(task.getName(), "stop");            _blockedTaskList.add(task);        }        _runnableTaskQueue.clear();        // Do the running task last.        if (_runningTask != null)        {            sendMessage(3,                        "Stopping task " +                        _runningTask.getName() +                        ".");            control.postMessage(_runningTask.getName(),                                "stop");            _blockedTaskList.add(_runningTask);            _runningTask = null;        }        return;    }    public void deleteRunningTask()    {        if (_runningTask != null)        {            _runningTask = null;        }        return;    }    // Forcibly delete all existing tasks with extreme prejudice.    public void deleteAllTasks()    {        _runningTask = null;        _runnableTaskQueue.clear();        _blockedTaskList.clear();        return;    }    public void exitApplication()    {        // Wait another 1.5 secs before actually dying.        setTimer("Exit", 1500);        return;    }    public Task findTask(String taskName)    {        Task retval = null;        // Is the running task the one we are looking for?        if (_runningTask != null &&            taskName.compareTo(_runningTask.getName()) == 0)        {            retval = _runningTask;        }        else        {            // Is the task in the runnable queue?            for (Task task: _runnableTaskQueue)            {                if (taskName.compareTo(task.getName()) == 0)                {                    retval = task;                    break;                }            }            // Is this task in the blocked list?            if (retval == null)            {                for (Task task: _blockedTaskList)                {                    if (taskName.compareTo(task.getName()) == 0)                    {                        retval = task;                        break;                    }                }            }        }        return(retval);    }    // Does a task already exist with this name?    public boolean taskExists(String name)    {        return(findTask(name) == null ? false : true);    }    // Issue the state machine transition associated with this    // timer name. Also, remove the now defunct timer from the    // timer table.    public void handleEvent(String eventName,                            Map<String, Object> args)    {        String taskName;        if (eventName.compareTo("Create Task") == 0)        {            Integer runtime;            Integer priority;            taskName = (String) args.get("Task Name");            runtime = (Integer) args.get("Runtime");            priority = (Integer) args.get("Priority");            createTask(taskName,                       runtime.intValue(),                       priority.intValue());        }        else if (eventName.compareTo("Run Task") == 0)        {            _fsm.RunTask();        }        else if (eventName.compareTo("Slice Timeout") == 0)        {            _fsm.SliceTimeout();        }        else if (eventName.compareTo("Reply Timeout") == 0)        {            _fsm.ReplyTimeout();        }        else if (eventName.compareTo("Suspend Task") == 0)        {            suspendTask();        }        else if (eventName.compareTo("Block Task") == 0)        {            taskName = (String) args.get("Task Name");            blockTask(taskName);        }        else if (eventName.compareTo("Unblock Task") == 0)        {            taskName = (String) args.get("Task Name");            unblockTask(taskName);        }        else if (eventName.compareTo("Delete Task") == 0)        {            taskName = (String) args.get("Task Name");            deleteTask(taskName);                    }        else if (eventName.compareTo("Task Suspended") == 0)        {            taskName = (String) args.get("Task Name");            sendMessage(2,                        "Task " +                        taskName +                        " has been suspended.");            _fsm.TaskSuspended();        }        else if (eventName.compareTo("Task Done") == 0)        {            taskName = (String) args.get("Task Name");            taskDone(taskName);        }        else if (eventName.compareTo("Task Stopped") == 0)        {            taskName = (String) args.get("Task Name");            taskStopped(taskName);        }        else if (eventName.compareTo("Task Deleted") == 0)        {            taskName = (String) args.get("Task Name");            taskDeleted(taskName);        }        else if (eventName.compareTo("Shutdown") == 0)        {            Integer exitCode;            exitCode = (Integer) args.get("Exit Code");            _exitCode = exitCode.intValue();            _fsm.Shutdown();        }        else if (eventName.compareTo("Exit") == 0)        {            System.exit(_exitCode);        }        else if (eventName.compareTo("ShutdownTimeout") == 0)        {            _fsm.ShutdownTimeout();        }        return;    }// Member Data.    private TaskManagerContext _fsm;    // Runnable task queue, sorted by priority.    private List<Task> _runnableTaskQueue;    // Blocked task list.    private List<Task> _blockedTaskList;    // The currently running task.    private Task _runningTask;    // Task manager's various timers.    private Map<String, Timer> _timerTable;    // The application's exit code.    private int _exitCode;// Inner classes.    private final class TimerListener        implements ActionListener    {        public TimerListener(String name,                             TaskManager owner)        {            _timerName = name;            _owner = owner;        }        public void actionPerformed(ActionEvent e)        {            Map<String, Object> args =                new HashMap<String, Object>();            _owner.handleEvent(_timerName, args);            return;        }        private String _timerName;        private TaskManager _owner;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -