Summary

Here are the methods of the Thread class that we introduced in this chapter:

void setPriority(int priority)

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.

int getPriority()

Retrieves the priority of the given thread.

void suspend() (deprecated in Java 2)

Prevents a thread from running for an indefinite amount of time.

void resume() (deprecated in Java 2)

Allows a thread to run after being suspended.

static void yield()

Yields the current thread, allowing another thread of the same priority to be run by the Java virtual machine.

void setDaemon(boolean on)

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset