Chapter 6. Java Thread Scheduling

At this point, we’ve covered the fundamental aspects of Java’s threading system and are able to write quite complex programs that exploit Java’s threads to complete their tasks. We’re now going to move into some of the specialized areas of threaded systems. The programming issues and techniques that we’ll explore in the next few chapters of this book are not issues you’ll grapple with every day, but when the need arises to have some explicit control over the behavior of your threads, these issues become very important.

To begin, in this chapter we’ll look into the topic of thread scheduling. In most Java programs, there are more threads than there are CPUs on the machine that is hosting the program. Since each CPU is capable of running only one thread at a time, not all threads in your program will be running at any given instant. Determining which of the many threads in your program is running at any one time is the topic of Java thread scheduling.

The key to understanding Java thread scheduling is to realize that a CPU is a scarce resource. When there are two or more threads that want to run on a single-processor machine, they end up competing for the CPU, and it’s up to someone — either the programmer, the Java virtual machine, or the operating system—to make sure that the CPU is shared between these threads. The same is true whenever there are more threads in a program than there are CPUs on the machine hosting the program. So the essence of ...

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.