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
priorityis 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
onis true) or to be a user thread (ifonis 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access