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

📄 taskmanager.java

📁 SMC takes a state machine stored in a .sm file and generates a State pattern in twelve programming l
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    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()    {        ListIterator taskIt;        Task task;        int index;        int taskIndex;        int taskPriority;        int currentMinPriority;        // Find the task with the lowest priority.        for (taskIt = _runnableTaskQueue.listIterator(0),                     currentMinPriority = Integer.MAX_VALUE,                     index = 0,                     taskIndex = -1;             taskIt.hasNext() == true;             ++index)        {            task = (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()    {        Iterator entryIt;        Map.Entry mapEntry;        Timer timer;        for (entryIt = _timerTable.entrySet().iterator();             entryIt.hasNext() == true;             )        {            mapEntry = (Map.Entry) entryIt.next();            timer = (Timer) mapEntry.getValue();            timer.stop();        }        _timerTable.clear();        return;    }    public void stopAllTasks()    {        TaskController control = new TaskController();        ListIterator listIt;        Task task;        // Put all tasks into the blocked list. As they report        // that they are stopped, remove the tasks.        //        // Do the blocked list first.        for (listIt = _blockedTaskList.listIterator(0);             listIt.hasNext() == true;             )        {            task = (Task) listIt.next();            sendMessage(3,                        "Stopping task " +                        task.getName() +                        ".");            control.postMessage(task.getName(),                                "stop");        }        // Do the runnable tasks next.        for (listIt = _runnableTaskQueue.listIterator(0);             listIt.hasNext() == true;             )        {            task = (Task) listIt.next();            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)    {        ListIterator taskIt;        Task task;        Task retval;        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 (taskIt = _runnableTaskQueue.listIterator(0);                 taskIt.hasNext() == true && retval == null;                )            {                task = (Task) taskIt.next();                if (taskName.compareTo(task.getName()) == 0)                {                    retval = task;                }            }            // Is this task in the blocked list?            if (retval == null)            {                for (taskIt = _blockedTaskList.listIterator(0);                     taskIt.hasNext() == true && retval == null;                    )                {                    task = (Task) taskIt.next();                    if (taskName.compareTo(task.getName()) == 0)                    {                        retval = task;                    }                }            }        }        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 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 LinkedList _runnableTaskQueue;    // Blocked task list.    private LinkedList _blockedTaskList;    // The currently running task.    private Task _runningTask;    // Task manager's various timers.    private Map _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 args = new HashMap();            _owner.handleEvent(_timerName, args);            return;        }        private String _timerName;        private TaskManager _owner;    }}

⌨️ 快捷键说明

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