CHAPTER 1. THE MENTAL LANDSCAPE 6
one thread to another, whether the thread that is currently running likes it or not. All
modern desktop and laptop computers use preemptive multitasking.
Ordinary users, and indeed ordinary programmers, have no need to deal with interrupts and
interrupt handlers. They can concentrate on the different tasks or threads th at they want the
computer to perform; the details of how the computer manages to get all those tasks done are
not important to them. In fact, most users, and many programmers, can ignore thre ads and
multitasking altogether. However, th r eads have become increasingly important as computers
have become more powerful and as they have b e gun to make more use of multitasking and
multiprocessing. In fact, the ability to work with threads is fast becoming an essential job skill
for programmers. Fortunately, Java has good support for threads, which are built into the Java
programming l anguage as a fundamental programming concept. Programming with threads
will be covered in Chapter 12.
Just as important in Java and in mo de r n programming in general is the basic concept of
asynchronous events. While programmers don’t actually deal with interrupts directly, they do
often find t he mse l ves writing event handl er s, which, like interrupt handler s, are called asyn-
chronously when specific events occur. Such “event-driven programming” has a very different
feel from the more traditional straight-through, synchronous programming. We will begin with
the more traditional type of programming, which is still used for programming individual tasks,
but we will return to threads and events later in the text, starting in
Chapter 6
∗ ∗ ∗
By the way, t he software that does all the interrupt handling, handles communication with
the user and with hardware devices, and controls which thread is allowed to run is called the
operating system. The operating system is the basic, essential software without which a
computer would not be able to function. Other programs, such as word processors and World
Wide Web browsers, are depe nde nt upon the operating system. Common operating systems
include Linux, W i n dows XP, Windows Vista, an d Mac OS.
1.3 The Java Virtual Machine
Machine language consists of very simple instructions that c an b e executed directly by
(online)
the CPU of a computer. Almost all programs, though , are writte n in high-level programming
languages such as Java, Pascal, or C+ + . A program written in a high-level language c annot
be run directly on any computer. First, it has to be translated into machine language. This
translation can be done by a program called a compiler . A compiler takes a high-level-language
program and translates it into an executable machine-language program. Once the translation
is done, the machine-language program can be run any number of times, but of course it can only
be run on one type of computer (sin ce each type of computer has its own individual machin e
language). If the program is to run on another type of computer it has to be re-translated,
using a different compiler, into the appropr i at e machine language.
There is an alternative to compiling a hi gh- l evel language program. Instead of using a
compiler, which translates the program all at once, you can use an interpreter , which translates
it instruction-by-instruction, as necessary. An interpreter is a program that acts much like a
CPU, with a kind of fetch-and-execute cycle. In order to execute a program, th e interpreter
runs in a loop in which it repeatedly reads one instruction from the program, decides what is
necessary to carry out that instruction, and then performs the appropriate machine-language
commands to do so.