Core Java 2: Volume II, Advanced Features, Fifth Edition

Core Java 2: Volume II, Advanced Features, Fifth Edition

by Cay S. Horstmann, Gary Cornell
     
 
  • The experienced developer's advanced guide to the Java 2 platform—fully updated for JDK 1.3 release and JDK 1.4 release, Standard Edition.
  • Even more robust, real-world code samples than ever before!
  • New and revamped coverage: XML, security, networking, multithreading, collections, remote objects, JDBC API, JavaBeans component architecture,

Overview

  • The experienced developer's advanced guide to the Java 2 platform—fully updated for JDK 1.3 release and JDK 1.4 release, Standard Edition.
  • Even more robust, real-world code samples than ever before!
  • New and revamped coverage: XML, security, networking, multithreading, collections, remote objects, JDBC API, JavaBeans component architecture, Swing, and much more
  • CD-ROM includes all the code examples, Forte for Java, Release 2.0, Community Edition, and the current version of the Java 2 SDK, Standard Edition for Windows, Solaris OE (SPARC/x86), and Linux.

The #1 advanced guide for serious programmers-fully updated for JDK 1.3 release and JDK 1.4 release.

An indispensable companion to the best-selling Core Java 2, Vol. I--Fundamentals (0-13-089468-0), Core Java 2, Volume II: Advanced Features is now available in a revised and expanded fifth edition. It delivers the same real-world guidance you need to solve even the most challenging programming problems and offers an all-new chapter on XML and Java, plus thoroughly revamped coverage of many advanced features—from collections to native methods, security to Swing.

Cay Horstmann identifies the problems experienced Java platform developers encounter most often, and delivers insightful, expert-level guidance for addressing them-together with even more of the robust, sample code that have made Core Java an international bestseller for five straight years. You'll gain new insights into networking, remote objects, JDBC API, internationalization, and a whole lot more.

For experienced programmers, Core Java 2, Volume 2: Advanced Features provides the answers that they need to take full advantage of the power of Java technology and to get the job done as efficiently as possible.

State-of-the-art information for advanced Java technology development, including:

  • Thoroughly updated coverage of multithreading, collections, and networking
  • Completely revised coverage of remote objects
  • Detailed new chapter on XML and Java
  • Sophisticated new techniques for utilizing JavaBeans(tm) component architecture
  • Advanced GUI-building techniques leveraging both Swing and AWT

About the CD-ROM

The accompanying CD-ROM contains complete source code examples, Forte for Java, Release 2.0, Community Edition, and the Java 2 SDK, Standard Edition.

Product Details

ISBN-13:
9780130927385
Publisher:
Prentice Hall Professional Technical Reference
Publication date:
12/10/2001
Series:
Core Series
Edition description:
Older Edition
Pages:
1232
Product dimensions:
7.04(w) x 9.22(h) x 1.76(d)

Read an Excerpt

Chapter 1: Multithreading

  • What are threads?
  • Interrupting threads
  • Thread properties
  • Thread priorities
  • Selfish threads
  • Synchronization
  • Deadlocks
  • User interface programming with threads
  • Using pipes for communication between threads
You are probably familiar with multitasking: the ability to have more than one program working at what seems like the same time. For example, you can print while editing or sending a fax. Of course, unless you have a multiple-processor machine, what is really going on is that the operating system is doling out resources to each program, giving the impression of parallel activity. This resource distribution is possible because while you may think you are keeping the computer busy by, for example, entering data, most of the CPU's time will be idle. (A fast typist takes around 1/20 of a second per character typed, after all, which is a huge time interval for a computer.)

Multitasking can be done in two ways, depending on whether the operating system interrupts programs without consulting with them first, or whether programs are only interrupted when they are willing to yield control. The former is called preemptive multitasking; the latter is called cooperative (or, simply, nonpreemptive) multitasking. Windows 3.1 and Mac OS 9 are cooperative multitasking systems, and UNIX/Linux, Windows NT (and Windows 95 for 32-bit programs), and OS X are preemptive. (Although harder to implement, preemptive multitasking is much more effective. With cooperative multitasking, a badly behaved program can hog everything.)

Multithreaded programs extend the idea of multitasking by taking it one level lower: individual programs will appear to do multiple tasks at the same time. Each task is usually called a thread—which is short for thread of control. Programs that can run more than one thread at once are said to be multithreaded. Think of each thread as running in a separate context: contexts make it seem as though each thread has its own CPU—with registers, memory, and its own code.

So, what is the difference between multiple processes and multiple threads? The essential difference is that while each process has a complete set of its own variables, threads share the same data. This sounds somewhat risky, and indeed it can be, as you will see later in this chapter. But it takes much less overhead to create and destroy individual threads than it does to launch new processes, which is why all modern operating systems support multithreading. Moreover, inter-process communication is much slower and more restrictive than communication between threads.

Multithreading is extremely useful in practice. For example, a browser should be able to simultaneously download multiple images. An email program should let you read your email while it is downloading new messages. The Java programming language itself uses a thread to do garbage collection in the background— thus saving you the trouble of managing memory! Graphical user interface (GUI) programs have a separate thread for gathering user interface events from the host operating environment. This chapter shows you how to add multithreading capability to your Java applications and applets.

Fair warning: multithreading can get very complex. In this chapter, we present all of the tools that the Java programming language provides for thread programming. We explain their use and limitations and give some simple but typical examples. However, for more intricate situations, we suggest that you turn to a more advanced reference, such as Concurrent Programming in Java by Doug Lea [Addison-Wesley 1999].


NOTE: In many programming languages, you have to use an external thread package to do multithreaded programming. The Java programming language builds in multithreading, which makes your job much easier.

What Are Threads?

Let us start by looking at a program that does not use multiple threads and that, as a consequence, makes it difficult for the user to perform several tasks with that program. After we dissect it, we will then show you how easy it is to have this program run separate threads. This program animates a bouncing ball by continually moving the ball, finding out if it bounces against a wall, and then redrawing it. (See Figure 1–1.)

As soon as you click on the "Start" button, the program launches a ball from the upper-left corner of the screen and the ball begins bouncing. The handler of the "Start" button calls the addBall method:

public void addBall()
{
try
{
Ball b = new Ball(canvas);
canvas.add(b);
for (int i = 1; i <= 1000; i++)
{
b.move();
Thread.sleep(5);
}
}
catch (InterruptedException exception)
{
}
}

That method contains a loop running through 1,000 moves. Each call to move moves the ball by a small amount, adjusts the direction if it bounces against a wall, and then redraws the canvas. The static sleep method of the Thread class pauses for 5 milliseconds.

The call to Thread.sleep does not create a new thread—sleep is a static method of the Thread class that temporarily stops the activity of the current thread. The sleep method can throw an InterruptedException. We will discuss this exception and its proper handling later. For now, we simply terminate the bouncing if this exception occurs.

If you run the program, the ball bounces around nicely, but it completely takes over the application. If you become tired of the bouncing ball before it has finished its 1,000 bounces and click on the "Close" button, the ball continues bouncing anyway. You cannot interact with the program until the ball has finished bouncing.


NOTE: If you carefully look over the code at the end of this section, you will notice the call

canvas.paint(canvas.getGraphics())

inside the move method of the Ball class. That is pretty strange—normally, you'd call repaint and let the AWT worry about getting the graphics context and doing the painting. But if you try to call canvas.repaint() in this program, you'll find out that the canvas is never repainted since the addBall method has completely taken over all processing. In the next program, where we use a separate thread to compute the ball position, we'll again use the familiar repaint.


Obviously, the behavior of this program is rather poor. You would not want the programs that you use behaving in this way when you ask them to do a time-consuming job. After all, when you are reading data over a network connection, it is all too common to be stuck in a task that you would really like to interrupt. For example, suppose you download a large image and decide, after seeing a piece of it, that you do not need or want to see the rest; you certainly would like to be able to click on a "Stop" or "Back" button to interrupt the loading process. In the next section, we will show you how to keep the user in control by running crucial parts of the code in a separate thread.

Example 1–1 is the entire code for the program....

Meet the Author

Cay S. Horstmann is Professor of Computer Science at San Jose State University. He has written six books on C++, Java technology, and object-oriented development, is series editor for Core books at Prentice Hall, and is a frequent speaker at computer industry conferences. A columnist for Java Report, he spent four years as Vice President and Chief Technology Officer of an Internet startup that grew from three people to a public company.

Gary Cornell has written or co-written over 20 popular computer books. He has a Ph.D. from Brown University and has been a visiting scientist at IBM Watson Labs, as well as a professor at the University of Connecticut.

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >