📄 ch08.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"><HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"><META NAME="GENERATOR" CONTENT="Adobe FrameMaker 6.0/HTML Export Filter"><LINK REL="STYLESHEET" HREF="CH08.css" CHARSET="ISO-8859-1" TYPE="text/css"><TITLE> Covered in this Chapter</TITLE></HEAD><BODY BGCOLOR="#ffffff"><P CLASS="CT"><A NAME="pgfId-1087399"></A>8</P><P CLASS="CT"><A NAME="pgfId-1087401"></A><A NAME="73141"></A>Performance Techniques</P><P CLASS="Body"><A NAME="pgfId-1087402"></A>One of the biggest challenges in developing large applications for the Java platform is to make the application meet its performance criteria. This chapter departs from the auction application and uses simple, targeted examples to show you how to track down performance bottlenecks to improve application performance.</P><DIV><H4 CLASS="A"><A NAME="pgfId-1087403"></A>Covered in this Chapter</H4><UL><LI CLASS="BL"><A NAME="pgfId-1087404"></A>Improving Applet Download Speed (page 247)</LI><LI CLASS="BL"><A NAME="pgfId-1087405"></A>Thread Pooling (page 250)</LI><LI CLASS="BL"><A NAME="pgfId-1087406"></A>Connection Pooling (page 252)</LI><LI CLASS="BL"><A NAME="pgfId-1087407"></A>Performance Features and Tools (page 263)</LI><LI CLASS="BL"><A NAME="pgfId-1087408"></A>Performance Analysis (page 269)</LI><LI CLASS="BL"><A NAME="pgfId-1087409"></A>Caching Client/Server Applications (page 277)</LI></UL></DIV><DIV><H4 CLASS="A"><A NAME="pgfId-1087411"></A><A NAME="71375"></A>Improving Applet Download Speed</H4><P CLASS="Body"><A NAME="pgfId-1087414"></A><A NAME="marker-1087412"></A><A NAME="marker-1087413"></A>Applet download performance refers to the time it takes for the browser to download all the files and resources it needs to start the applet. An important factor affecting any applet's download performance is the number of times it has to request data from the server. You can reduce the number of requests by packaging the applet images into one class file, or using Java Archive (JAR) files. </P><DIV><H5 CLASS="B"><A NAME="pgfId-1087415"></A>Packaging Images into One Class</H5><P CLASS="Body"><A NAME="pgfId-1087418"></A><A NAME="marker-1087416"></A><A NAME="marker-1087417"></A>Normally, if an applet has, for example, six image buttons, that translates to six additional requests sent back to the Web server to load those image files. Six additional requests might not seem like much on an internal network, but given connections of lesser speed and reliability, those additional requests can have a significant negative impact on performance. So, your ultimate goal should be to load the applet as quickly as possible. </P><P CLASS="Body"><A NAME="pgfId-1087420"></A>One way to store images in a class file is to use an ASCII encoding scheme such as <EM CLASS="A">X-PixMap (XPM) (</EM><A NAME="marker-1087419"></A><EM CLASS="URL-Footnote">http://www.inria.fr/koala/lehors/xpm.html</EM><EM CLASS="A">).</EM> This way, rather than maintaining the images as GIF files on the server, the files are encoded as <EM CLASS="CODE">Strings</EM> and stored in a single class file. </P><P CLASS="Body"><A NAME="pgfId-1087421"></A>This code sample uses packages from the JavaCup winner at JavaOne 1996, which contains the <A NAME="marker-1087422"></A><EM CLASS="CODE">XImageSource</EM> and <EM CLASS="CODE">XpmParser</EM><A NAME="marker-1087423"></A> classes. These classes provide all you need to read a standard XPM file. You can see these files at <EM CLASS="A">SunSite (</EM><EM CLASS="URL-Footnote">http://sunsite.utk.edu/winners_circle/developer_tools/DESVS7NU/applet.html</EM>). </P><P CLASS="Body"><A NAME="pgfId-1087424"></A>For the initial encoding process, there are a number of graphics tools you can use to create XPM files. On Solaris you can use <EM CLASS="CODE">ImageTool</EM> or a variety of other <EM CLASS="A">GNU image packages (</EM><EM CLASS="URL-Footnote">http://www.gnu.ai.mit.edu/software/software.html</EM>). Go to the <EM CLASS="CODE">Download.com</EM> Web site (<EM CLASS="URL-Footnote">http://download.cnet.com</EM>) to get the encoding software for Windows platforms. </P><P CLASS="Body"><A NAME="pgfId-1087425"></A>The following code excerpted from the <EM CLASS="CODE">MyApplet</EM> sample class loads the images shown in Figure 8.1. You can see the coded String form for the images in the <EM CLASS="CODE">XPM Definition</EM> of the images. </P><DIV><H6 CLASS="FC"><A NAME="pgfId-1087430"></A>Figure 8.1 <A NAME="53087"></A>Images</H6><DIV><IMG SRC="CH08-1.gif"></DIV><P CLASS="Body"><A NAME="pgfId-1087432"></A>The <EM CLASS="CODE">Toolkit</EM><A NAME="marker-1087431"></A> class creates an <EM CLASS="CODE">Image</EM> object for each image from the <EM CLASS="CODE">XPM Image Source</EM> object. The parameters to <EM CLASS="CODE">XImageSource</EM> represent a coded <EM CLASS="CODE">String</EM> form from the <EM CLASS="CODE">XPM Definition</EM>.</P><PRE CLASS="CODE"><A NAME="pgfId-1087433"></A>Toolkit kit = Toolkit.getDefaultToolkit();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087434"></A>Image image;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087435"></A>mage = kit.createImage (new XImageSource (_reply));</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087436"></A>image = kit.createImage (new XImageSource (_post));</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087437"></A>image = kit.createImage (new XImageSource (_reload));</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087438"></A>image = kit.createImage (new XImageSource (_catchup));</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087439"></A>image = kit.createImage (new XImageSource (_back10));</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087440"></A>image = kit.createImage (new XImageSource (_reset));</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087441"></A>image = kit.createImage (new XImageSource (_faq));</PRE><P CLASS="Body"><A NAME="pgfId-1087442"></A>The alternative technique below uses GIF files. It requires a request back to the Web server for each image loaded. </P><PRE CLASS="CODE"><A NAME="pgfId-1087443"></A>Image image; image = getImage (reply.gif); </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087444"></A>image = getImage (post.gif);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087445"></A>image = getImage (reload.gif);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087446"></A>image = getImage (catchup.gif);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087447"></A>image = getImage (back10.gif);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087448"></A>image = getImage (reset.gif);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087449"></A>image = getImage (faq.gif);</PRE><P CLASS="Body"><A NAME="pgfId-1087450"></A>This technique reduces network traffic because all images are available in a single class file. </P><UL><LI CLASS="BL"><A NAME="pgfId-1087451"></A>Using XPM encoded images makes the class size larger but the number of network requests fewer. </LI><LI CLASS="BL"><A NAME="pgfId-1087452"></A>Making the XPM image definitions part of your applet class file makes the image loading process part of the regular loading of the applet class file with no extra classes.</LI></UL><P CLASS="Body"><A NAME="pgfId-1087453"></A>Once loaded, you can use the images to create buttons or other user interface components. This next code segment shows how to use the images with the <EM CLASS="CODE">javax.swing.JButton</EM> class. </P><PRE CLASS="CODE"><A NAME="pgfId-1087454"></A>ImageIcon icon = new ImageIcon(kit.createImage(new XImageSource(_reply)));</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087455"></A>JButton button = new JButton (icon, Reply);</PRE></DIV></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087456"></A>Using JAR Files</H5><P CLASS="Body"><A NAME="pgfId-1087457"></A>When an applet consists of more than one file, you can improve download performance with <A NAME="marker-1087458"></A>JAR files. A JAR file contains all of an applet's related files in one single file for a faster download. Much of the time saved comes from reducing the number of HTTP connections the browser must make. Chapter 9, Deploying the Auction Application, has information on creating and signing JAR files. </P><P CLASS="Body"><A NAME="pgfId-1087460"></A>The HTML code below uses the <EM CLASS="CODE">CODE</EM><A NAME="marker-1087459"></A> tag to specify the executable for the <EM CLASS="CODE">MyApplet</EM> applet, and the <EM CLASS="CODE">ARCHIVE</EM><A NAME="marker-1087461"></A> tag to specify the JAR file that contains all of <EM CLASS="CODE">MyApplet's</EM> related files. The executable specified by the <EM CLASS="CODE">CODE</EM> tag is sometimes called the code base. </P><P CLASS="Body"><A NAME="pgfId-1087462"></A>For security reasons the JAR files listed by the archive parameter must be in the same directory or a subdirectory as the applet's code base. If no <EM CLASS="CODE">codebase</EM> parameter is supplied, the directory from where the applet was loaded is used as the code base. The following example specifies <EM CLASS="CODE">jarfile</EM> as the JAR file that contains the related files for the <EM CLASS="CODE">MyApplet.class</EM> executable. </P><PRE CLASS="CODE"><A NAME="pgfId-1087463"></A><APPLET CODE=MyApplet.class ARCHIVE=jarfile WIDTH=100 HEIGHT=200> </APPLET> </PRE><P CLASS="Body"><A NAME="pgfId-1087464"></A>The applet downloads the entire JAR file, regardless of whether or not the JAR file includes infrequently used files.</P><PRE CLASS="CODE"><A NAME="pgfId-1087465"></A><APPLET CODE=MyApplet.class ARCHIVE=jarfile1, jarfile2 WIDTH=100 HEIGHT=200> </APPLET> </PRE><P CLASS="Body"><A NAME="pgfId-1087466"></A>To improve performance when an applet has infrequently used files, put the frequently used files into the JAR file and the infrequently used files into the applet class directory. Infrequently used files are then located and downloaded by the browser only when needed. </P></DIV></DIV><DIV><H4 CLASS="A"><A NAME="pgfId-1087468"></A><A NAME="37513"></A>Thread Pooling</H4><P CLASS="Body"><A NAME="pgfId-1087469"></A>Bandwidth restrictions imposed on networks around the world make network-based operations potential bottlenecks that can have a significant impact on an application's performance. Many network-based applications are designed to use connection pools so they can reuse existing network connections and save on the time and overhead invested in opening and closing network connections. </P><P CLASS="Body"><A NAME="pgfId-1087472"></A><A NAME="marker-1087470"></A><A NAME="marker-1087471"></A>The Java Developer Connection (JDC) applet servers and the Java Web Server make extensive use of thread pooling to improve performance. Thread pooling is creating a ready supply of sleeping threads at the beginning of execution. Thread creation and their startup process is expensive, so run time performance is improved with thread pooling because threads are not created during run time; they are simply reused.<A NAME="marker-1087473"></A><A NAME="marker-1087474"></A><A NAME="marker-1087475"></A> </P><P CLASS="Body"><A NAME="pgfId-1087476"></A>This code sample taken from the <EM CLASS="CODE">Pool</EM> class (page 282) shows one way to implement thread pooling. In the pool's constructor (shown below), the <EM CLASS="CODE">WorkerThread</EM>s are initialized and started. The call to the <EM CLASS="CODE">start</EM> method executes the <EM CLASS="CODE">run</EM> method of the <EM CLASS="CODE">WorkerThread</EM><A NAME="marker-1087477"></A>, and the call to <EM CLASS="CODE">wait</EM> in the <EM CLASS="CODE">run</EM> method suspends the <EM CLASS="CODE">Thread</EM> while the <EM CLASS="CODE">Thread</EM> waits for work to arrive. The last line of the constructor pushes the sleeping <EM CLASS="CODE">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -