Previous Table of Contents Next


14.1.1.9. Multithreaded

In a GUI-based network application such as a Web browser, it’s easy to imagine multiple things going on at the same time. A user could be listening to an audio clip while she is scrolling a page and in the background the browser is downloading an image. Java is a multithreaded language; it provides support for multiple threads of execution (sometimes called lightweight processes) that can handle different tasks. An important benefit of multithreading is that it improves the interactive performance of graphical applications for the user.

If you have tried working with threads in C or C++, you know that it can be quite difficult. Java makes programming with threads much easier, by providing built-in language support for threads. The java.lang package provides a Thread class that supports methods to start and stop threads and set thread priorities, among other things. The Java language syntax also supports threads directly with the synchronized keyword. This keyword makes it extremely easy to mark sections of code or entire methods that should only be run by a single thread at a time.

While threads are “wizard-level” stuff in C and C++, their use is commonplace in Java. Because Java makes threads so easy to use, the Java class libraries require their use in a number of places. For example, any applet that performs animation does so with a thread. Similarly, Java does not support asynchronous, non-blocking I/O with notification through signals or interrupts—you must instead create a thread that blocks on every I/O channel you are interested in.

14.1.1.10. A Simple Example

By now, you should have a pretty good idea of why Java is such an interesting language. So we’ll stop talking about abstract concepts and look at some concrete Java code. Before we look at an interesting applet, however, we are going to pay tribute to that ubiquitous favorite, “Hello World.”

Hello World

Example 14.1 shows the simplest possible Java program: “Hello World.”

EXAMPLE 14.1. Hello World.

   public class HelloWorld {
      public static void main(String[] args) {
         System.out.println(“Hello World!”);
        }
   }

This program, like every Java program, consists of a public class definition. The class contains a method named main(), which is the main entry point for all Java applications—that is, the point at which the interpreter starts executing the program. The body of main() consists of only a single line, which prints out the message:

   Hello World!

This program must be saved in a file with the same name as the public class plus a .java extension. To compile it, you would use javac:2


2Assuming you’re using Sun’s Java Development Kit (JDK). If you’re using a Java development environment from some other vendor, follow your vendor’s instructions.
   % javac HelloWorld.java

This command produces the HelloWorld.class file in the current directory. To run the program, you use the Java interpreter, java:

   % java HelloWorld

Note that when you invoke the interpreter, you do not supply the .class extension for the file you want to run.

A Scribble Applet

Example 14.2 shows a less trivial Java program. This program is an applet, rather than a stand-alone Java application like the “Hello World” program above. Because this example is an applet, it has a different structure than a stand-alone application; notably, it does not have a main() method. Like all applets, this one runs inside an applet viewer or Web browser, and lets the user draw (or scribble) with the mouse, as illustrated in Figure 14.1.


FIGURE 14.1.  A Java applet running in a Web browser.


Previous Table of Contents Next