📄 perf.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Writing Advanced Applications, Chapter 8: Improving Performance</TITLE>
<META NAME="AUTHOR" CONTENT="Monica Pawlan and Calvin Austin">
<META NAME="KEYWORDS" CONTENT="programming, advanced, Java 2">
<META NAME="OWNER" CONTENT="Editorial/JDC">
<META NAME="revision" CONTENT="@(#)perf.src 1.18 09/21/99 JDC">
</HEAD>
<!-- Start Body Insert-->
<BODY BGCOLOR="#ffffff">
<!-- End Body Insert-->
<!-- Start PageTop Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR ALIGN="CENTER" VALIGN="TOP">
<TD WIDTH="157" ALIGN="LEFT">
<IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT="">
<A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A>
<BR>
<IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD>
<TD>
<FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD ALIGN="RIGHT">
<IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD WIDTH="100%">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD BGCOLOR="#CC9966" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="LEFT">
<IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="CENTER">
<A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD>
<TD VALIGN="CENTER">
<FONT FACE="Helvetica" SIZE="1">
<INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD>
<TD VALIGN="CENTER">
<INPUT TYPE="image" SRC="/images/search.button.gif"
value="search" BORDER="0" WIDTH="55" HEIGHT="14" ALT="Search"></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</FORM>
<P>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">
<TR VALIGN="TOP">
<TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55"
HEIGHT="18" ALT=""></TD>
<TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152">
<A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" ALT="Online Training" BORDER=0></A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<!-- End PageTop Insert -->
<!-- Start NavBar Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT">
<!-- tab categories -->
<TR>
<TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News & Events from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD>
</TR>
<TR><TD> </TD></TR>
<!-- End NavBar Insert -->
<!-- START SUB-NAV -->
<TR>
<TD><!-- INSERT SUB-NAV INFO -->
</TD>
</TR>
<!-- END SUB-NAV -->
</TABLE>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD>
<!-- Template Version 2.0 -->
<!-- ================== -->
<!-- Start Main Content -->
<!-- ================== -->
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="TOP">
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="top"></A>
<DIV ALIGN="RIGHT">
<FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT>
<H2>Writing Advanced Applications<BR>
<FONT SIZE="3">Chapter 8 Continued: Improving Performance By Design</FONT></H2>
<FONT SIZE="-1">[<A HREF="perfTech.html"><<BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="conpool.html">NEXT>></A>]</FONT></DIV>
<P>
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>
Besides connection pooling, there are other features you can design into your
programs to improve performance. This chapter explains how you can design an
applet to download files and resources more efficiently, or design a
thread-based program to use thread pooling to save on the expensive thread
startup process.
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#download">Improving Applet Download Speed</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#pool">Thread Pooling</A></FONT>
</UL>
<A NAME="download"></A>
<H3>Improving Applet Download Speed</H3>
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<FONT SIZE="-2"><SUP>TM</SUP></FONT> ARchive (JAR)
files.
<H4>Packaging Images into One Class</H4>
Normally, if an applet has 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>
One way to store images in a class file is to use an ASCII encoding scheme
such as
<A HREF="http://www.inria.fr/koala/lehors/xpm.html">X-PixMap (XPM).</A>
This way, rather than maintaining the images as GIF files on the server, the
files are encoded as <CODE>Strings</CODE> and stored in a single class file.
<P>
This code sample uses packages from the JavaCup winner at JavaOne 1996, which
contains the <CODE>XImageSource</CODE> and <CODE>XpmParser</CODE> classes.
These classes provide all you need to read a standard <CODE>XPM</CODE> file.
You can see these files at
<A HREF="http://sunsite.utk.edu/winners_circle/developer_tools/DESVS7NU/applet.html">SunSite</A>.
<P>
For the initial encoding process, there are a number of graphics tools
you can use to create <CODE>XPM</CODE> files. On Solaris you can use
<CODE>ImageTool</CODE> or a variety of other
<A HREF="http://www.gnu.ai.mit.edu/software/software.html">GNU image
packages</A>. Go to the <A HREF="http://download.com/">Download.com</A>
web site to get the encoding software for Windows platforms.
<P>
The following code excerpted from the
<A HREF="./Code/MyApplet.java">MyApplet</A> sample class loads the images.
You can see the coded <CODE>String</CODE> form for the images in the
<A HREF="Images.html">XPM definition</A> of the images.
<P>
<IMG SRC="/images/Images.gif" HSPACE="5" VSPACE="5" BORDER="2" HEIGHT="59" WIDTH="371">
<P>
The <CODE>Toolkit</CODE> class creates an <CODE>Image</CODE> object for each
image from the XPM Image Source object.
</FONT>
<PRE><FONT SIZE="-1">
Toolkit kit = Toolkit.getDefaultToolkit();
Image image;
image = kit.createImage (new XImageSource (_reply));
image = kit.createImage (new XImageSource (_post));
image = kit.createImage (new XImageSource (_reload));
image = kit.createImage (new XImageSource (_catchup));
image = kit.createImage (new XImageSource (_back10));
image = kit.createImage (new XImageSource (_reset));
image = kit.createImage (new XImageSource (_faq));
</FONT>
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P>
The alternative technique below uses GIF files. It requires a request back
to the web server for each image loaded.
</FONT>
<PRE><FONT SIZE="-1">
Image image;
image = getImage ("reply.gif");
image = getImage ("post.gif");
image = getImage ("reload.gif");
image = getImage ("catchup.gif");
image = getImage ("back10.gif");
image = getImage ("reset.gif");
image = getImage ("faq.gif");
</FONT>
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
This technique reduces network traffic because all images are available in
a single class file.
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Using XPM encoded images makes the class size larger,
but the number of network requests fewer. </FONT>
<P>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">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.</FONT>
</UL>
<P>
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 <CODE>javax.swing.JButton</CODE> class.
<PRE>
ImageIcon icon = new ImageIcon (
kit.createImage (
new XImageSource (_reply)));
JButton button = new JButton (icon, "Reply");
</PRE>
<H4>Using JAR Files</H4>
When an applet consists of more than one file, you can improve download
performance with Java ARchive (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.
<P>
<!-- <A HREF="deploy.html"> -->
Chapter 9: Deploying Your Application has information on creating and signing
JAR files.
<P>
The HTML code below uses the <CODE>CODE</CODE> tag to specify the executable
for the <CODE>MyApplet</CODE> applet, and the <CODE>ARCHIVE</CODE> tag to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -