?? 08-03.html
字號:
<INPUT name="fields" type=hidden value="vdkvgwkey score vstitle vsisbn vsauthor vspublisher vspubdate">
<INPUT name="imageprefix" type=hidden value="http://academic.itknowledge.com">
<INPUT name="ssiFolder" type=hidden value="itkaca">
<INPUT name="topics" type=hidden value="itk_academic">
<INPUT type="hidden" name="bookid" value="t_1562438425">
<font face="arial, helvetica" size=2><b>Search this book:</b></font><br>
<INPUT NAME="query" size=25 VALUE=""> <input type="image" width=28 height=23 border=0 value="Go" name="Go" src="/images/go.gif" align=absmiddle>
</form>
<!-- Empty Reference Subhead -->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="08-02.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch09/09-01.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading19"></A><FONT COLOR="#000077">An Example of Programming with ThreadGroups</FONT></H3>
<P>The following is an example of an Applet that continually lists all of the threads that are operating in the Java VM. It shows how to obtain the root ThreadGroup of the system and how to (recursively) enumerate all of the ThreadGroups that are descendants of the root ThreadGroup.
</P>
<P><B><SMALL>LISTTHREADS.JAVA</SMALL></B></P>
<TABLE BORDER="2" BORDERCOLOR="#0000" WIDTH="90%" ALIGN="CENTER">
<TR><TD>
<!-- CODE //-->
<PRE>
import java.applet.*;
import java.awt.*;
class ListThreads extends Applet implements Runnable
{
LTFrame m_frame;
Thread m_thread;
boolean m_bShutdown = false;
long m_ulSleepInterval = 5000;
public void init() {
{
m_frame = new LTFrame(”ThreadLister”, this);
m_frame.show();
m_thread = new Thread(this);
m_thread.start();
}
public void stop ()
{
}
public void destroy()
{
super.stop();
}
public void terminate()
{
m_bShutdown = true;
if (m_thread != null)
{
m_thread.stop();
m_thread = null;
}
}
public void run()
{
while (!m-bShutdown)
{
m_frame.getListbox().clear();
showThreadGroupsRecursively(getRootThreadGroup(), 0);
try
{
Thread.sleep(m_ulSleepInterval);
}
catch (InterruptedException ie)
{
return;
}
catch (NullPointerException ne)
{
return;
catch (Exception e)
{
return;
}
}
}
public Thread getThread()
{
return m_thread;
}
protected synchronized void
showThreadGroupsRecursively(ThreadGroup tg, int level)
{
if (tg == null)
return;
String strItem = new String();
for (int i = 0; i <level; i++)
strItem += “ ”;
strltem += tg.toString();
m_frame.getListbox().addItem(strItem);
int nGroups = tg.activeGroupCount ();
if (nGroups == 0)
return;
ThreadGroup aThreadGroups[] = new ThreadGroup[nGroups];
tg.enumerate(aThreadGroups, false);
for (int i = 0; i < nGroups; i++)
{
showThreadGroupsRecursively(aThreadGroups[i], level+1);
}
}
protected void showThreadGroups()
{
List 1stGroups = m_frame.getListbox();
1stGroups.clear();
ThreadGroup aThreadGroups[] = getListOfThreadGroups();
if (aThreadGroups == null)
{
1stGroups.addItem(”There are no ThreadGroups here”);
return;
}
for (int i = 0; i < aThreadGroups.length; i++)
{
ThreadGroup tg = aThreadGroups[i];
1stGroups.addItem(tg.toStringo());
}
}
protected ThreadGroup[] getListOfThreadGroups()
{
ThreadGroup tgRoot = getRootThreadGroupo();
if (tgRoot == null)
return null;
int nGroups = tgRoot.activeGroupCounto () + 1;
if (nGroups == 1)
return null;
ThreadGroup aThreadGroups[] = new ThreadGroup[nGroups];
tgRoot.enumerate(aThreadGroups, true);
for (int i = nGroups-1; i > 0; i--)
{
aThreadGroups[i] = aThreadGroups[i-1];
}
aThreadGroups[0] = tgRoot;
return aThreadGroups;
}
public ThreadGroup getRootThreadGroup()
{
ThreadGroup tg = Thread.currentThread().getThreadGroup();
ThreadGroup tgParent = tg;
do
{
tg = tgParent;
tgParent = tgParent.getParent();
n } while (tgParent != null);
return tg;
}
}
class LTFrame extends Frame
{
protected ListThreads m_app;
protected List m_listThreads;
protected Button m_btnQuit;
protected Butt m_btnSuspendPolling;
protected boolean m_bSuspended = false;
public LTFrame(String szTitle, ListThreads app)
{
super(szTitle);
m_app = app;
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
// Put a listbox in the top half of the frame
m_listThreads = new List(10, false);
// Set up the GridBagConstraints so that the listbox
// resizes both vertically and horizontally. The listbox
// is anchored at the top of the frame.
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 2;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weighty = 1.0;
gbc.weightx = 1.0;
layout.setConstraints(m_listThreads, gbc);
add(m_listThreads);
// Create a panel which will be placed on the bottom
// part of the frame
Panel panel = new Panel();
// Set up the GridBagConstraints so that the button bar does
// not resize vertically, but *does* resize horizontally.
// The button bar is anchored at the bottom of the frame.
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;
gbc.gridheight = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.SOUTHWEST;
gbc.weighty = 0.0;
gbc.weightx = 1.0;
layout.setConstraints(panel, gbc);
panel.setBackground(Color.blue);
add(panel);
// Create a button and add it to the bottom panel
FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
panel.setLayout(flowLayout);
m_btnQuit = new Button(“Quit”);
panel.add(m_btnQuit);
m_btnSuspendPolling = new Button(“Suspend Polling”);
panel.add(m_btnSuspendPolling);
pack(); // components get their preferred size
}
public List getListbox()
{
return m_listThreads;
}
public boolean action(Event evt, Object what)
{
if (evt.target == m_btnQuit)
{
hide();
dispose();
m_app.terminate();
return true;
}
else if (evt.target == m_btnSuspendPolling)
{
if (m_bSuspended) // If already suspended, resume
{
m_app.getThreado().resume();
m-btnSuspendPolling.setLabel(”Suspend Polling”);
}
else
{
m_app.getThread().suspend();
m_btnSuspendPolling.setLabel(”Resume Polling”);
}
m_bSuspended = !m_bSuspended;
return true;
}
return false;
}
}
</PRE>
<!-- END CODE //-->
</TD>
</TR>
</TABLE>
<P><B>Lab 3: Enumerating Threads</B></P>
<P>Perform the following steps:</P>
<DL>
<DD><B>1.</B> Take the above example and extend it so that each thread within each ThreadGroup is enumerated and listed in hierarchical fashion.
<DD><B>2.</B> Add two buttons that allow you to increase or decrease the priority of the highlighted thread.
<DD><B>3.</B> Ensure that you catch any SecurityException that might occur if you exceed the minimum or maximum bounds of the thread’s priority limit.
</DL>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="08-02.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch09/09-01.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/ddc00001.html">DDC Publishing</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --><!-- BEGIN SUB FOOTER --> <br> <img src="/images/dotclear.gif" width="5" height="7" border="0"> </TD> </TR> </TABLE> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="left" width="160"><img src="/images/bot_curve.jpg" width="160" alt="" border="0"></td> <td align="left" valign="top" nowrap><a href="/"><img src="/images/top_tabs/home_bot.gif" alt="home" border="0"></a><!-- <a href="/content/corp.html"><img src="/images/top_tabs/subscribe_bot.gif" alt="Subscribe" border="0"></a> --><a href="/search/"><img src="/images/top_tabs/search_bot.gif" alt="search" border="0"></a><a href="/faq/faq.html"><img src="/images/top_tabs/faq_bot.gif" alt="faq" border="0"></a><a href="/sitemap.html"><img src="/images/top_tabs/sitemap_bot.gif" alt="sitemap" border="0"></a><a href="/contactus.html"><img src="/images/top_tabs/contact_us_bot.gif" alt="contactus" border="0"></a><img src="/images/dotclear.gif" width=260 height="1" alt="" border="0"></td> </tr></table> <table width="100%" bgcolor="#003366" border=0 cellpadding=0 cellspacing=0> <tr> <td align="left" width=145><img src="/images/dotclear.gif" width=145 height="1" alt="" border="0"></td> <!-- END SUB FOOTER -->
<!-- all of the books have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --><!-- FOOTER --> <td align="left" bgcolor="#003366"><table border="0" cellspacing="10" cellpadding="5"><tr><td align="center"><font face="arial, helvetica" size="1" color="#cccccc"><b><a href="/products.html"><font color="#0099CC">Products</font></a> | <a href="/contactus.html"><font color="#0099CC">Contact Us</font></a> | <a href="http://www.earthweb.com/dlink.corp|about_us-jhtml.72.0.-.0.jhtml" target="resource window"><font color="#0099CC">About Us</font></a> | <a href="http://www.earthweb.com/dlink.corp|privacy-jhtml.72.0.-.-.jhtml" target="resource window"><font color="#0099CC">Privacy</font></a> | <a href="http://www.itmarketer.com/" target="resource window"><font color="#0099CC">Ad Info</font></a> | <!--<a href="/consortia/"><font color="#0099CC">Consortia</font></a> | --><a href="/"><font color="#0099CC">Home</font></a></b><br><br>Use of this site is subject to certain <a href="/agreement.html"><font color="#0099CC">Terms & Conditions</font></a>, <a href="/copyright.html"><font color="#0099CC">Copyright © 1996-2000 EarthWeb Inc.</font></a> All rights reserved. Reproduction in whole or in part in any form or medium without express written <a href="http://www.earthweb.com/dlink.corp|permissions-jhtml.72.0.-.-.jhtml" target="resource window"><font color="#0099CC">permission</font></a> of EarthWeb is prohibited. Read EarthWeb's <A HREF="http://www.earthweb.com/dlink.corp|privacy-jhtml.72.0.-.-.jhtml" target="resource window"><font color="#0099CC">privacy</font></A> statement.</font><br><br></td></tr></table><a href="AITK1a2b3c4d5e6f7g8h9idefcon4.html"><img src="/images/dotclear.gif" border="0" height="1" width="1" align="left"></a></td> </tr></table><!--DoubleClick Ad BEGIN--><SCRIPT LANGUAGE="JavaScript"><!--document.write('<layer src="http://ad.doubleclick.net/adl/academic.itknowledge.com/homepage;cat=homepage;cat=enterprise;cat=education;cat=it_training;ord=' + ord + '" width="468" height="60" visibility="hide" onload="moveToAbsolute(ph1.pageX, ph1.pageY); visibility=\'show\';" clip="468,60"></layer>');document.write('<LAYER SRC="http://ad.doubleclick.net/adl/itkaca.earthweb.dart/b_aca_soft_dev;a=b_aca_soft_dev4;sz=160x60;ord=' + ord + '" width=160 height=60 visibility="hidden" onLoad="moveToAbsolute(layer1.pageX,layer1.pageY);clip.height=60;clip.width=160; visibility=\'show\';"></LAYER>');//--></SCRIPT> <!--DoubleClick Ad END--></BODY></HTML><!-- END FOOTER -->
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -