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 ...

Get Java Threads, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.