CHAPTER 1. THE MENTAL LANDSCAPE 5
Interrupts allow the CPU to deal with asynchronous events. In the regular fetch-and-
execute cycle, things happen in a predetermined order; everything that happens is “synchro-
nized” with everything else. Interrupts make it possible for the CPU to deal efficiently with
events that happen “asynchronously,” that is, at unpredictable times.
As another ex ampl e of how interr u pts are used, consider what happens when the CPU needs
to access data that is stored on a hard disk. The CPU can access data directly only if it is
in main memory. Data on the disk has to be copied i nto memory before it can be accessed.
Unfortunately, on the scale of speed at which the CPU operates, the disk drive is extremely
slow. When the CPU needs data from the disk, it sends a signal to the disk d ri ve telling it
to locate the data and get it ready. (This signal is sent synchronously, under the control of
a regular program.) Then, instead of just waitin g the long and unpredictable amount of time
that the disk drive will take to do this, the CPU goes on with some other task. When the disk
drive has the data ready, it sends an interrupt signal to the CPU. The interrupt handler can
then read the requested data.
∗ ∗ ∗
Now, you might have noticed th at all this only makes sense if the CPU actually has several
tasks to perform. If it has nothing better to do, it might as well spend its time polling for input
or waiting for disk drive operations to complete. All modern computers use multitasking to
perform several tasks at once. Some computers can be used by several people at once. Since the
CPU is so fast, it can quickly switch its attention from one user to another, devoting a fraction
of a second to e ach user in turn . This application of multitasking i s called timesharing . But a
modern personal computer with just a single user also uses multitasking. For example, the user
might be typing a paper while a clock is continuously displaying the time and a file is being
downloaded ove r the network.
Each of t he individual tasks that the CP U is working on is called a thread. (Or a process;
there ar e technical differences between t hr ead s and processes, but they are not important here,
since it is thr e ads that are used in Java.) Many CPUs can literally execute more than one
thread simul t ane ousl y —s uch CPUs contain multiple “cores,” each of which can run a thread—
but there is always a limit on the number of threads that can be executed at the same time.
Since there are often more threads than can be executed simultaneously, the computer has to be
able switch its attention from one thread to anot he r, just as a timesharing computer switches
its attention from one user to another. In general, a thread that is being executed will continue
to run until one of several things happens:
• The thread might voluntarily yield control, to give other threads a chance to run.
• The thread might have to wait for some asynchronous event to occur. For example, the
thread might request some data from the disk drive, or it might wait for the user to press
a key. W hi l e i t is waiting, the thread is said to be blocked, and other threads, if any, have
a chance to run. When the event occurs, an interrupt will “wake up” the thread so that
it can continue running.
• The thread might use up its allotted slice of time and be suspended to allow other threads
to run. Not all computers can “forcibly” susp en d a thread in this way; those that can are
said to us e preemptive multitasking. To do pree mpt i ve multitasking, a computer needs
a special timer device that generates an interrupt at regular intervals, such as 100 times
per second. When a timer interrupt occurs, the CPU has a chance to switch from one
thread to another, whethe r the thread that is currently running likes it or not. All modern
desktop and laptop computers, and even typical smartphones and tablets, use preemptive