コード例 #1
0
  /*
   *  Create a thread object using the default constructor ThreadCB() and
   *  associate this newly created thread with a task (provided as an
   *  argument to the do create() method).
   *
   */
  public static ThreadCB do_create(TaskCB task) {
    // It is important to keep in mind that each time control is transferred to
    // the operating system, it is seen as an opportunity to schedule a thread
    // to run. Therefore, regardless of whether the new thread was created
    // successfully, the dispatcher must be called (or else a warning will be issued)

    // There is a global constant (in IflThreadCB), called MaxThreadsPerTask.
    // If this number of threads per task is exceeded, no new thread should be
    // created for that task, and null should be returned.
    if (MaxThreadsPerTask <= task.getThreadCount()) {
      dispatch();
      return null;
    }

    // To link a thread to its task, the method addThread() of class
    // IflTaskCB should be used and the thread’s task must be set using
    // the method setTask() of IflThreadCB.*/
    ThreadCB thread = new ThreadCB();

    // null should also be returned if addThread() returns FAILURE. You can
    // find out the number of threads a task currently has by calling the
    // method getThreadCount() on that task.
    if (task.addThread(thread) == FAILURE) {
      dispatch();
      return null;
    }

    // Apparently I needed to add this after testing. 2 hours of debugging. Wow
    task.addThread(thread);

    thread.setTask(task);

    // If priority scheduling needs to be implemented, the do create() method must cor-
    // rectly assign the thread’s initial priority. The actual value of the priority depends
    // on the particular scheduling policy used. OSP 2 provides methods for setting and
    // querying the priority of both tasks and threads. The methods are setPriority() and
    // getPriority() in classes TaskCB and ThreadCB, respectively.
    thread.setPriority(randomGeneratedNumber.nextInt((10 - 1) + 1));

    // Finally, the status of the new thread should be set to ThreadReady
    // and it should be placed in the ready queue.
    thread.setStatus(ThreadReady);

    // If priority is above below 10% or equal to move into the low priority thread.
    if (task.getPriority() <= 1) ThreadCB.lowPriorityQueue.addElement(thread);
    else ThreadCB.highPriorityQueue.addElement(thread);

    // If all is well, the thread object created by this method should be returned.
    dispatch();
    return thread;
  }