📄 perf.html
字号:
specify the JAR file that contains all of <CODE>MyApplet's</CODE> related
files. The executable specified by the <CODE>CODE</CODE> tag is sometimes
called the <CODE>code base</CODE>.
<P>
For security reasons the JAR files listed by the <CODE>archive</CODE>
parameter must be in the same directory or a sub-directory as the
applets <CODE>codebase</CODE>. If no <CODE>codebase</CODE> parameter
is supplied the directory from where the applet was loaded is used as
the <CODE>codebase</CODE>.
<P>
The following example specifies <CODE>jarfile</CODE> as the JAR file that
contains the related files for the <CODE>MyApplet.class</CODE> executable.
<P>
<APPLET CODE="MyApplet.class" ARCHIVE="jarfile"
WIDTH="100" HEIGHT="200">
</APPLET>
<P>
If the applet download uses multiple JAR files as shown in the next HTML
segment, the <CODE>ClassLoader</CODE> loads each JAR file when the applet
starts. So, if your applet uses some resource files infrequently, the JAR
file containing those infrequently used files is downloaded, regardless of
whether the resources are actually used during that session or not.
<P>
<APPLET CODE="MyApplet.class" ARCHIVE="jarfile1, jarfile2"
WIDTH="100" HEIGHT="200">
</APPLET>
<P>
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.
<A NAME="pool"></A>
<H3>Thread Pooling</H3>
The Java Developer Connection<FONT SIZE="-2"><SUP>SM</SUP></FONT>
(JDC) applet servers and the Java Web Server<FONT SIZE="-2"><SUP>TM</SUP></FONT>
make extensive use of thread pooling to improve performance. Thread pooling
is creating a ready supply of sleeping threads at the beginning of execution.
Because the thread startup process is expensive in terms of system resources,
thread pooling makes the startup process a little slower, but improves runtime
performance because sleeping (or suspended) threads are awakened only when
they are needed to perform new tasks.
<P>
This code sample taken from the <A HREF="./Code/Pool.java">Pool.java</A>
class shows one way to implement thread pooling. In the pool's constructor
(shown below), the <CODE>WorkerThreads</CODE> are initialized and started.
The call to the <CODE>start</CODE> method executes the <CODE>run</CODE> method
of the <CODE>WorkerThread</CODE>, and the call to <CODE>wait</CODE> in the
<CODE>run</CODE> method suspends the <CODE>Thread</CODE> while the
<CODE>Thread</CODE> waits for work to arrive. The last line of the constructor
pushes the sleeping <CODE>Thread</CODE> onto the stack.
</FONT>
<PRE><FONT SIZE="-1">
public Pool (int max, Class workerClass)
throws Exception {
_max = max;
_waiting = new Stack();
_workerClass = workerClass;
Worker worker;
WorkerThread w;
for ( int i = 0; i < _max; i++ ) {
worker = (Worker)_workerClass.newInstance();
w = new WorkerThread ("Worker#"+i, worker);
w.start();
_waiting.push (w);
}
}
</FONT>
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P>
Besides the <CODE>run</CODE> method, the <CODE>WorkerThread</CODE> class has
a <CODE>wake</CODE> method. When work comes in, the <CODE>wake</CODE> method
is called, which assigns the data and notifies the sleeping
<CODE>WorkerThread</CODE> (the one initialized by the <CODE>Pool</CODE>)
to resume running. The <CODE>wake</CODE> method's call to <CODE>notify</CODE>
causes the blocked <CODE>WorkerThread</CODE> to fall out of its wait state,
and the <CODE>run</CODE> method of the
<A HREF="./Code/HttpServerWorker.java">HttpServerWorker</A> class is executed.
Once the work is done, the <CODE>WorkerThread</CODE> is either put back onto
the <CODE>Stack</CODE> (assuming the Thread <CODE>Pool</CODE> is not full) or
terminates.
</FONT>
<PRE><FONT SIZE="-1">
synchronized void wake (Object data) {
_data = data;
notify();
}
synchronized public void run(){
boolean stop = false;
while (!stop){
if ( _data == null ){
try{
wait();
}catch (InterruptedException e){
e.printStackTrace();
continue;
}
}
if ( _data != null ){
_worker.run(_data);
}
_data = null;
stop = !(_push (this));
}
}
</FONT>
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
At its highest level, incoming work is handled by the <CODE>performWork</CODE>
method in the <CODE>Pool</CODE> class (shown below). As work comes in, an
existing <CODE>WorkerThread</CODE> is popped off of the <CODE>Stack</CODE>
(or a new one is created if the <CODE>Pool</CODE> is empty). The sleeping
<CODE>WorkerThread</CODE> is then activated by a call to its <CODE>wake</CODE>
method.
</FONT>
<PRE><FONT SIZE="-1">
public void performWork (Object data)
throws InstantiationException{
WorkerThread w = null;
synchronized (_waiting){
if ( _waiting.empty() ){
try{
w = new WorkerThread ("additional worker",
(Worker)_workerClass.newInstance());
w.start();
}catch (Exception e){
throw new InstantiationException (
"Problem creating
instance of Worker.class: "
+ e.getMessage());
}
}else{
w = (WorkerThread)_waiting.pop();
}
}
w.wake (data);
}
</FONT>
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
The <A HREF="./Code/HttpServer.java">HttpServer.java</A> class constructor
creates a new <CODE>Pool</CODE> instance to service
<A HREF="./Code/HttpServerWorker.java">HttpServerWorker</A> instances.
<CODE>HttpServerWorker</CODE> instances are created and stored as part of the
<CODE>WorkerThread</CODE> data. When a <CODE>WorkerThread</CODE> is activated
by a call to its <CODE>wake</CODE> method, the <CODE>HttpServerWorker</CODE>
instance is invoked by way of its <CODE>run</CODE> method.
</FONT>
<PRE><FONT SIZE="-1">
try{
_pool = new Pool (poolSize,
HttpServerWorker.class);
}catch (Exception e){
e.printStackTrace();
throw new InternalError (e.getMessage());
}
</FONT>
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P>
This next code is in the <CODE>run</CODE> method of the
<A HREF="./Code/HttpServer.java">HttpServer.java</A> class. Every time a
request comes in, the data is initialized and the <CODE>Thread</CODE> starts
work.
<BLOCKQUOTE>
<HR>
<STRONG>Note:</STRONG> If creating a new <CODE>Hashtable</CODE> for
each <CODE>WorkerThread</CODE> presents too much overhead, just modify
the code so it does not use the <CODE>Worker</CODE> abstraction.
<HR>
</BLOCKQUOTE>
</FONT>
<PRE><FONT SIZE="-1">
try{
Socket s = _serverSocket.accept();
Hashtable data = new Hashtable();
data.put ("Socket", s);
data.put ("HttpServer", this);
_pool.performWork (data);
}catch (Exception e){
e.printStackTrace();
}
</FONT>
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
Thread pooling is an effective performance-tuning technique that puts the
expensive thread startup process at the startup of an application. This way,
the negative impact on performance occurs once at program startup where it is
least likely to be noticed.
<HR>
<P ALIGN="RIGHT">
<FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT>
</FONT>
</TD>
</TR>
</TABLE>
<!-- ================ -->
<!-- End Main Content -->
<!-- ================ -->
</TD>
</TR>
</TABLE>
<!-- Copyright Insert -->
<BR CLEAR="ALL">
<FORM ACTION="/cgi-bin/search.cgi" METHOD="POST">
<TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5">
<TR>
<TD VALIGN="TOP">
<P ALIGN=CENTER>
<FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif">
[ This page was updated: <!-- new date --> 13-Oct-99 ]</font></P>
</TD>
</TR>
<TR>
<TD BGCOLOR="#CCCCCC">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<CENTER>
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="http://java.sun.com/products/">Products & APIs</A> |
<A HREF="/developer/index.html">Developer Connection</A> |
<A HREF="/developer/infodocs/index.shtml">Docs & Training</A> |
<A HREF="/developer/support/index.html">Online Support</A><BR>
<A HREF="/developer/community/index.html">Community Discussion</A> |
<A HREF="http://java.sun.com/industry/">Industry News</A> |
<A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> |
<A HREF="http://java.sun.com/casestudies">Case Studies</A>
</FONT>
</CENTER>
</TD>
</TR>
<TR>
<TD BGCOLOR="#CCCCCC">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD ALIGN="CENTER">
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> -
<A HREF="http://java.sun.com/applets/">Applets</A> -
<A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> -
<A HREF="http://java.sun.com/jobs/">Employment</A> -
<A HREF="http://java.sun.com/nav/business/">Business & Licensing</A> -
<A HREF="http://java.sun.com/javastore/">Java Store</A> -
<A HREF="http://java.sun.com/casestudies/">Java in the Real World</A>
</FONT>
</TD>
</TR>
<TR>
<TD>
<CENTER>
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
<a href="/siteinfo/faq.html">FAQ</a> |
<a href="/feedback/index.html">Feedback</a> |
<a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> |
<A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A>
</FONT>
</CENTER>
</TD>
</TR>
<TR>
<TD>
<TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0">
<TR>
<TD WIDTH="50%">
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
For more information on Java technology<BR>
and other software from Sun Microsystems, call:<BR>
</FONT>
<FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif">
(800) 786-7638<BR></FONT>
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
Outside the U.S. and Canada, dial your country's
<A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&T Direct Access Number</A> first.<BR>
</FONT>
</TD>
<TD ALIGN="RIGHT" WIDTH="50%">
<A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR>
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
Copyright © 1995-99
<A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR>
All Rights Reserved.
<a href="http://www.sun.com/share/text/SMICopyright.html">Legal Terms</a>.
<A HREF="http://www.sun.com/privacy/">Privacy Policy</A>.
</FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</FORM>
<!-- End Copyright Insert -->
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -