Here are the methods of the Thread class that we introduced in this chapter:
Sets the priority of the given thread. If priority
is outside the allowed range of thread priorities, an exception is
thrown. However, if the priority is within the allowed range of
thread priorities but greater than the maximum priority of the
thread’s thread group, then the priority is silently lowered to
match that maximum priority.
Retrieves the priority of the given thread.
Prevents a thread from running for an indefinite amount of time.
Allows a thread to run after being suspended.
Yields the current thread, allowing another thread of the same priority to be run by the Java virtual machine.
Sets the thread to be a daemon thread (if on
is
true) or to be a user thread (if on
is false).
We’ve spent a lot of time in this chapter discussing the priority and scheduling of threads. Scheduling is one of the gray areas of Java programming because actual scheduling models are not defined by the Java specification, which means that scheduling behavior may vary from platform to platform. The reason for this is Java’s quest for simplicity: since the scheduling model of a program rarely affects the ultimate outcome or usefulness of that program, Java leaves the added complexity of explicit scheduling to the developer in those cases where the scheduling is important.
Accordingly, implementations of the Java virtual machine differ in the way they handle thread scheduling. The simplest model—the green-thread model—follows a rather strict priority-based scheduling algorithm; models that are implemented on top of operating-system-specific thread libraries follow the basic precepts of that algorithm, but they each take into account other factors when deciding which thread to run.
In the next chapter, we’ll take a look at some scheduling techniques based on simple priority-based scheduling.