26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 15 Multithreading 839<br />

[Note: On many computer platforms, C and C++ programs can perform multithreading by<br />

using system specific code libraries.]<br />

Software Engineering Observation 15.1<br />

Unlike many languages that do not have built-in multithreading (such as C and C++) and must<br />

therefore make calls <strong>to</strong> operating system multithreading primitives, <strong>Java</strong> includes multithreading<br />

primitives as part of the language itself (actually in classes Thread, ThreadGroup,<br />

ThreadLocal and ThreadDeath of the java.lang package). This encourages the use<br />

of multithreading among a larger part of the applications-programming community. 15.1<br />

We will discuss many applications of concurrent programming. When programs<br />

download large files such as audio clips or video clips from the World Wide Web, we do<br />

not want <strong>to</strong> wait until an entire clip is downloaded before starting the playback. So we can<br />

put multiple threads <strong>to</strong> work: One that downloads a clip and another that plays the clip so<br />

that these activities, or tasks, may proceed concurrently. To avoid choppy playback, we will<br />

coordinate the threads so that the player thread does not begin until there is a sufficient<br />

amount of the clip in memory <strong>to</strong> keep the player thread busy.<br />

Another example of multithreading is <strong>Java</strong>’s au<strong>to</strong>matic garbage collection. In C and<br />

C++, the programmer is responsible for reclaiming dynamically allocated memory. <strong>Java</strong><br />

provides a garbage collec<strong>to</strong>r thread that reclaims dynamically allocated memory that the<br />

program no longer needs.<br />

Testing and Debugging Tip 15.1<br />

In C and C++, programmers must provide explicit statements that reclaim dynamically allocated<br />

memory. When memory is not reclaimed (because a programmer forgets <strong>to</strong> do so,<br />

because of a logic error or because an exception diverts program control), this results in an<br />

all-<strong>to</strong>o-common error called a memory leak that can eventually exhaust the supply of free<br />

memory and may cause premature program termination. <strong>Java</strong>’s au<strong>to</strong>matic garbage collection<br />

eliminates the vast majority of memory leaks, that is, those that are due <strong>to</strong> orphaned (unreferenced)<br />

objects. 15.1<br />

<strong>Java</strong>’s garbage collec<strong>to</strong>r runs as a low-priority thread. When <strong>Java</strong> determines that there<br />

are no longer any references <strong>to</strong> an object, it marks the object for eventual garbage collection.<br />

The garbage-collec<strong>to</strong>r thread runs when processor time is available and when there are<br />

no higher priority runnable threads. <strong>How</strong>ever, the garbage collec<strong>to</strong>r will run immediately<br />

when the system is out of memory.<br />

Performance Tip 15.1<br />

Setting an object reference <strong>to</strong> null marks that object for eventual garbage collection (if<br />

there are no other references <strong>to</strong> the object). This can help conserve memory in a system in<br />

which a local variable that refers <strong>to</strong> an object does not go out of scope because the method<br />

in which it appears executes for a lengthy period. 15.1<br />

Writing multithreaded programs can be tricky. Although the human mind can perform<br />

many functions concurrently, humans find it difficult <strong>to</strong> jump between parallel “trains of<br />

thought.” To see why multithreading can be difficult <strong>to</strong> program and understand, try the following<br />

experiment: open three books <strong>to</strong> page 1. Now try reading the books concurrently.<br />

Read a few words from the first book, then read a few words from the second book, then<br />

read a few words from the third book, then loop back and read the next few words from the<br />

first book, and so on. After a brief time, you will appreciate the challenges of multithreading:<br />

switching between books, reading briefly, remembering your place in each

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!