📄 ch8.htm
字号:
<HTML><HEAD><TITLE>developer.com - Reference</TITLE><META NAME="Author" CONTENT="developer.com"><META NAME="Keywords" CONTENT="javascript, ActiveX, VRML, Push, channel,push channel, applet, directory, news, community, developer, internet,intranet, java, java applet, J++, javabeans, java products, tools, javatools, JDK, java development kit, java development environment, JIT,JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources, API,browser, java documentation, CDF, Common Desktop Format, RDF, ResourceDescription Framework, Netcaster, Netcast, Castanet, meta-language, metadata,solaris, Solaris, SUN, CGI, Perl, Oracle, database, network, Netscape API,Apache, apache, SSL, mSQL, Netscape, Fasttrack, FastTrack,industry standard, ActiveX bridge, web, internet, site, sitedesign, microsoft, active server pages, asp, iis, nt, win95, 95, html, cgi,frontpage, business, corporate, vbscript, javascript, activex, isapi,database, access, sql, server, www, 3.0, 3.01, 3.02, 3.03, 3.04, 4.0,4.0b2, 4.01, 4.03, 4.04, connection, online, commerce, publisher,publishing, publish, webmaster, dynamic html, dhtml, windows 98, netscape,netscape software, shareware, magazines, jobs, japan, free, freeware, freestuff"><META NAME="description" CONTENT="Developer.com is the leading onlineservice for developers. It includes Gamelan, the official directory for Java, as wellas news, information, tutorials, and directories for other Internet technologiesincluding ActiveX, JavaScript, perl, VRML, Java Beans, push technologies, andother Internet and intranet technologies. Developer.com is also the home ofDeveloper Direct, the Professional Developer's Store, where developers canpurchase and download thousands of Web, authoring and other developmentproducts at some of today's lowest prices."><META NAME="distribution" CONTENT="global"><META NAME="resource-type" CONTENT="document"><META NAME="language" CONTENT="en"></HEAD><BODY BACKGROUND="/images/curve_r-wide.gif" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#DD0000" VLINK="#999999" ALINK="#FF0000"><TABLE><TR><TD VALIGN=TOP WIDTH=120><A HREF="/" ><IMG SRC="/images/masthead_r.gif" WIDTH="100" HEIGHT="78" BORDER="0"ALT="Developer.com Logo"></a></TD><TD><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCBAN.REF/DT=null"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCBAN.REF/DT=null" HEIGHT="60" WIDTH="468" ALT="Click here to support our advertisers"><BR><CENTER>Click here to support our advertisers</CENTER></a></TD></TR></TABLE><!--outer table--><TABLE><!--outer table--><TR><!--outer table--><TD VALIGN="TOP"><!--navbar info --><table><tr><td><A HREF="http://www.developerdirect.com/" ><IMG SRC="/images/button_s.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>SHOPPING</b></font></a></td></tr><tr><td><A HREF="/community/jobs/c_jobs.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>JOB BANK</B></font></a></td></tr><tr><td><A HREF="http://www.classifieds2000.com/cgi-cls/display.exe?Developer+computer+searchcomputercategories"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>CLASSIFIEDS</B></font></a></td></tr><tr><td><A HREF="/directories/directories.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>DIRECTORIES</B></font></a></td></tr><tr><td><A HREF="/reference/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>REFERENCE</B></font></a><br><a href="/reference/r_library.html"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">Online Library</a></font></td></tr><tr><td><A HREF="/classroom/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>LEARNING CENTER</B></font></a></td></tr><tr><td><A HREF="/journal/"><IMG SRC="/images/button_n.gif" width="15" height="11" BORDER="0"><B><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">JOURNAL</font></B></a></td></tr><tr><td><A HREF="/news/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>NEWS CENTRAL</b></font></a></td></tr><tr><td><A HREF="/downloads/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>DOWNLOADS</b></font></a></td></tr><tr><td><A HREF="/community/" ><IMG SRC="/images/button_c.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>COMMUNITY</B></font></a></td></tr><tr><td><A HREF="/calendar/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>CALENDAR</b></font></a></td></tr><tr><td><A HREF="/about/"><IMG SRC="/images/button_a.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>ABOUT US</b></a></font></td></tr></table><table><tr><td><form action="/subscribe/list.cgi" method=post ><img src="/images/rule.gif" width="100" height="2"><br><B><FONT FACE="Arial, Helvetica" SIZE="-1">Journal:</b></font><br><input name='email' value='your e-mail' size="11"><input type='hidden' name='state' value='Subscribe'></td></tr><tr><td><input type='submit' value='Subscribe'><br><font size="-1">Get the weekly email highlights from the most popular journal for developers!<br><a href="http://www.developer.com/subscribe.html">Current issue</a><img src="/images/rule.gif" width="100" height="2"><input type='hidden' name='name' value='New Subscriber'></FORM></td></tr></table><!--/navbar--><font size="-1"><A HREF="http://www.developer.com">developer.com</a><BR><A HREF="http://www.developerdirect.com">developerdirect.com</a><BR><A HREF="http://www.htmlgoodies.com">htmlgoodies.com</a><BR><A HREF="http://www.javagoodies.com">javagoodies.com</a><br><A HREF="http://www.jars.com">jars.com</a><BR><a href="http://www.intranetjournal.com">intranetjournal.com</a><br><a href="http://www.javascripts.com">javascripts.com</a><br></font><!--outer table--></TD><!--outer table--><TD VALIGN="TOP"><BLOCKQUOTE><P><img src="/images/header_r.gif" width="440" height="50"><P><MAP NAME ="header_r2.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/author.html" COORDS ="361,0,440,29"><AREA SHAPE ="RECT" HREF ="/reference/basic.html" COORDS ="254,0,360,29"><AREA SHAPE ="RECT" HREF ="/reference/help.html" COORDS ="183,0,251,29"><AREA SHAPE ="RECT" HREF ="/reference/cool.html" COORDS ="100,0,181,29"><AREA SHAPE ="RECT" HREF ="/reference/mybook.html" COORDS ="0,0,100,29"></MAP><MAP NAME ="header_r1.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/personalize.html" COORDS ="339,0,439,29"></MAP><p><font face="Arial,Helvetica" size="-1" color="#006666"><A HREF="/reference/r_library.html"><B>All Categories</A> :</B><b><A HREF="/reference/r_java.html">Java</A></b></font><p> <META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]"><H1><FONT COLOR=#FF0000>Chapter 8</FONT></H1><H1><B><FONT SIZE=5 COLOR=#FF0000>Adding Threads to Applets</FONT></B></H1><P><HR WIDTH="100%"></P><P><H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A></FONT></FONT></H3><UL><LI><A HREF="#WhatIsaThread" >What Is a Thread?</A><LI><A HREF="#CreatingaThreadwiththeThreadClass" >Creating a Thread with the Thread Class</A><UL><LI><A HREF="#EnhancingYourFirstMultithreadedApple" >Enhancing Your First Multithreaded Applet</A></UL><LI><A HREF="#TheRunnableInterface" >The Runnable Interface</A><LI><A HREF="#Synchronization" >Synchronization</A><UL><LI><A HREF="#ATestStackClassThatIsNotThreadSaf" >A TestStack Class That Is Not Thread-Safe</A><LI><A HREF="#IntroducingtheSynchronizedModifier" >Introducing the Synchronized Modifier</A><LI><A HREF="#NotifyandWait" >Notify and Wait</A></UL><LI><A HREF="#MoreAboutThreads" >More About Threads</A><LI><A HREF="#ThreadGroups" >ThreadGroups</A><UL><LI><A HREF="#ThreadDeath" >ThreadDeath</A></UL><LI><A HREF="#TalkingThreadsPipesandThreads" >Talking Threads: Pipes and Threads</A><LI><A HREF="#ChapterProject" >Chapter Project</A><UL><LI><A HREF="#ClassOrganization" >Class Organization</A><LI><A HREF="#CatalogHTMLandPreloadFile" >Catalog HTML and Preload File</A></UL><LI><A HREF="#TheMediaLoaderThreadandMediaObserver" >The MediaLoaderThread and MediaObserver Classes</A><UL><LI><A HREF="#TheMediaLoaderClass" >The MediaLoader Class</A><LI><A HREF="#TheMediaSweeperClass" >The MediaSweeper Class</A><LI><A HREF="#TheQueueClass" >The Queue Class</A><LI><A HREF="#TheBMPImageClass" >The BMPImage Class</A><LI><A HREF="#TheCatalogClass" >The Catalog Class</A><LI><A HREF="#TheCatalogButtonClass" >The CatalogButton Class</A><LI><A HREF="#TheSelectionCanvasClass" >The SelectionCanvas Class</A></UL><LI><A HREF="#Summary" >Summary</A></UL><HR><P>Using images might make your applet look nicer, but it also slowsthe performance of your applet quite a bit. The biggest reasonfor this delay is that images are large compared to normal textdocuments, such as HTML files; consequently, images take longerto load from the network. Another reason for this delay is thetime it takes to render some images. Most images are based ona graphics format that uses some type of compression algorithm.Although this compression can result in faster delivery, renderingthe image will take additional time, even though this time delaymay be minor.<P>In traditional programming, you would have no options for speedingup the image display. You would simply have to wait for each imageto be downloaded, then wait for it to be rendered. However, thischapter ends Part III of this book by introducing <I>multithreading</I>as a way to make your applet run faster and more efficiently.No longer will you have to wait for each image to be downloadedand rendered. With multithreading, you can download images whileother images are being displayed. The prospective user of yourapplet won't have to wait for all the images to be downloadedto make a decision. With multithreading, you can use a singleimage when it's ready instead of waiting for all images to bemade available.<H2><A NAME="WhatIsaThread"><FONT SIZE=5 COLOR=#FF0000>What Isa Thread?</FONT></A></H2><P>A <I>thread</I> has many features of a standalone process. Ithas a beginning, a middle, and an ending. Technically speaking,a thread is a single sequential stream of execution. A threadcan perform a task until it has run to its conclusion (such asa long calculation), or it can run indefinitely, waiting for requeststo perform a service (such as a database server). In practicalterms, a thread can do just about everything a process can.<P>So what is special about threads? The key thing to note is thata thread runs inside a process; to put it another way, a processcan contain one or more threads (in an operating environment thatsupports multithreading). This means that one process can havea thread that performs a calculation, another thread that performsa service, and so forth. Furthermore, threads can create new threads.In Java, threads are everywhere. There are even threads you haven'texplicitly created. The object garbage collector runs in the virtualmachine as a thread. Even the Applet class that starts your appletis itself running as part of a thread.<P>There are other subtle differences between processes and threads.Multitasking operating systems, which have been around for a longtime, allow multiple processes to run simultaneously. However,these processes are generally completely independent of each other;the only thing they share is the operating environment. Consequently,activities like getting processes to communicate with each othercan be a little tricky. In a multithreading environment, however,programs that would traditionally be run as separate processescan be structured to run as separate threads within a common process.This allows the threads to share resources common to that process,such as objects and variables. Consequently, the threads of aprocess, though running separately, also work together to form
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -