Chapter 7. Java Thread Scheduling Examples

The thread methods that we looked at in the last chapter are great when you have a fixed number of well-known threads and can analyze the behavior of the threads in advance. The priority-based scheduling methods also were most useful when there were intermediate results in which the user might be interested. But there are times when you have independent threads that need a round-robin timesliced behavior regardless of the platform on which they ‘re running. There are also times when it’s convenient to create multiple threads, but you want to prevent the round-robin timesliced behavior you’d get on some platforms. And, because some platforms cannot handle large numbers of threads, there are cases when you want to limit the number of threads that your program uses.

We’ll look at these issues in this chapter, and we’ll provide four examples of how general-purpose scheduling can be achieved in a Java program. We start with the notion of a thread pool : a pool of a limited number of threads that can each run multiple objects in succession. A thread pool is useful for programs that may require more threads than can reasonably fit within a particular virtual machine.

Next, we move on to describe two round-robin-based schedulers: a very simple scheduler (most suitable for green-thread implementations) and a more generalized scheduler that is suitable for both ensuring and preventing round-robin scheduling. We present this scheduler for two reasons: ...

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.