_chapter 1.htm
来自「Core Java 2(中文名称:JAVA 2 核心技术 卷二:高级特性)这是英」· HTM 代码 · 共 144 行
HTM
144 行
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Chapter 1</title>
<link rel="stylesheet" type="text/css" href="docsafari.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<ul></ul>
<table width="100%" border="1" bgcolor="#EBEBFF">
<tr>
<td width="5%" align="left" valign="middle"><a href="Front%20matter.htm"><img src="Larrow.gif" width="17" height="19" border="0"></a></td>
<td align="center" valign="middle"><a class="docLink" href="Front%20matter.htm">CONTENTS</a></td>
<td width="5%" align="right" valign="middle"><a href="_chapter%202.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td>
</tr>
</table>
<h2 class="docChapterTitle">Chapter 1. Multithreading</h2>
<ul>
<li>
<p class="docList"><a class="docLink" href="#c1s1">What Are Threads?</a></li>
<li>
<p class="docList"><a class="docLink" href="#c1s2">Interrupting Threads</a></li>
<li>
<p class="docList"><a class="docLink" href="#c1s3">Thread Properties</a></li>
<li>
<p class="docList"><a class="docLink" href="#c1s4">Thread Priorities</a></li>
<li>
<p class="docList"><a class="docLink" href="#c1s5">Selfish Threads</a></li>
<li>
<p class="docList"><a class="docLink" href="#c1s6">Synchronization</a></li>
<li>
<p class="docList"><a class="docLink" href="#c1s7">Deadlocks</a></li>
<li>
<p class="docList"><a class="docLink" href="#c1s8">User Interface Programming with Threads</a></li>
<li>
<p class="docList"><a class="docLink" href="#c1s9">Using Pipes for Communication between Threads</a></li>
</ul>
<p class="docText">You are probably familiar with <span class="docEmphasis">
multitasking:</span> 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.)</p>
<p class="docText">Multitasking can be done in two ways, depending on whether
the operating system interrupts programs without consulting with them first, or
whether pro-grams are only interrupted when they are willing to yield control.
The former is called <span class="docEmphasis">preemptive multitasking;</span>
the latter is called <span class="docEmphasis">cooperative</span> (or, simply,
nonpreemptive) <span class="docEmphasis">multitasking</span> 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.)</p>
<p class="docText">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 <span class="docEmphasis">thread</span>梬hich
is short for thread of control. Programs that can run more than one thread at
once are said to be <span class="docEmphasis">multithreaded.</span> Think of
each thread as running in a separate context: contexts make it seem as though
each thread has its own CPU梬ith registers, memory, and its own code.</p>
<p class="docText">So, what is the difference between multiple
<span class="docEmphasis">processes</span> and multiple
<span class="docEmphasis">threads?</span> 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.</p>
<p class="docText">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梩hus 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.</p>
<p class="docText">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 <i>Concurrent Programming in
Java</i> by Doug Lea [Addison-Wesley 1999].</p>
<div class="docNote">
<p class="docNoteTitle">NOTE</p>
<table cellSpacing="0" cellPadding="1" width="90%" border="0">
<tr>
<td vAlign="top" width="60">
<img alt="graphics/note.gif" src="note.gif" align="left" border="0" width="54" height="53"><br>
</td>
<td vAlign="top">
<p class="docText">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.</td>
</tr>
</table>
</div>
<h3 class="docSection1Title" id="c1s1">What Are Threads?</h3>
<p class="docText">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
<a class="docLink" href="#ch01fig01">Figure 1-1</a>.)</p>
<center>
<h5 id="ch01fig01" class="docFigureTitle">Figure 1-1. Using a thread to animate a bouncing ball</h5>
<p>
<img alt="graphics/01fig01.jpg" src="01fig01.jpg" border="0" width="461" height="378"></p>
</center>
<p class="docText">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 <tt>addBall</tt> method:</p>
<pre>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)
{
}
}
</pre>
<p class="docText">That method contains a loop running through 1,000 moves. Each
call to <tt>move</tt> moves the ball by a small amount, adjusts the direction if
it bounces against a wall, and then redraws the canvas. The static <tt>sleep</tt>
method of the <tt>Thread</tt> class pauses for 5 milliseconds.</p>
<p class="docText">The call to <tt>Thread.sleep</tt> does not create a new
thread
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?