📄 103-107.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:Java Programming: The Basics</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=2//-->
<!--PAGES=103-107//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="097-103.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="107-108.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Start the <I>DigitalThreads</I> class by making an instance, setting the frame and graphics variables, and then invoking the <I>run</I> method:</P>
<!-- CODE SNIP //-->
<PRE>
1. public static DigitalThreads
2. clock_thread = new DigitalThreads();
3. void start_clock() {
4. clock_thread.g = getGraphics();
5. clock_thread.f = main_frame;
6. clock_thread.start();
7. }
</PRE>
<!-- END CODE SNIP //-->
<P>Note that in line 4 the graphics context is obtained from the local graphics context. This assumes that the graphics context for the instance of the class exists. Also, note that a <I>main_frame</I> variable was available for use in line 5. In fact, the clock could be placed in any frame and graphics context. Notice that the clock will update the main frame even if it becomes hidden by other frames. The clock thread does not check when its frame is hidden.</P>
<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">Thread Groups</FONT></H4>
<P>One of the issues facing programmers who want to create many threads is that of limited system resources. CPU time is a limited resource, and we need to keep an eye on the CPU usage and space requirements of the started threads. This information is critical, and it is needed by both the program designer and the program itself. The designer requires this information in order to engineer a program that reacts well to the changing load on a system.
</P>
<P>Every thread belongs to a <I>thread group</I>. The thread groups are arranged in a thread group hierarchy, at the root of which is the <I>system thread group</I>. The system thread group consists of four threads: the garbage collector, the clock handler, the idle thread, and the finalizer thread. The <I>main group</I>, a child of the system thread group, has the default thread that starts at the <I>main()</I> method. When windows are started, new threads are added to the main group. These threads include, but are not limited to, the AWT input thread, AWT toolkit thread, and screen updater thread.</P>
<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">The Thread Manager</FONT></H4>
<P>This section shows how to write a program that will enable the display of the various threads running on a system. At the heart of the thread management concept is the idea that we need to access the system thread group (i.e., the root thread) and then list all the children. The procedure is to create a thread and then traverse up the parent groups until we reach the root group, which is identified by its null parentage. The following code returns a reference to the current thread.
</P>
<!-- CODE SNIP //-->
<PRE>
Thread.currentThread()
</PRE>
<!-- END CODE SNIP //-->
<P>This code returns a reference to the group that the current thread belongs to:
</P>
<!-- CODE SNIP //-->
<PRE>
Thread.currentThread().getThreadGroup()
</PRE>
<!-- END CODE SNIP //-->
<P>The reference is of <I>ThreadGroup</I> type. Once we have an instance of the current thread group, we can obtain the parent of the current thread group, which is also a thread group. We continue this traversal until the parent of the current thread group is null.</P>
<P>The following routine returns the <I>SystemThreadGroup</I>:</P>
<!-- CODE //-->
<PRE>
public ThreadGroup getSystemThreadGroup() {
ThreadGroup systemThreadGroup;
ThreadGroup parentThreadGroup;
systemThreadGroup = Thread.currentThread().getThreadGroup();
while ( ( parentThreadGroup = systemThreadGroup.getParent())
!= null)
systemThreadGroup = parentThreadGroup;
return systemThreadGroup;
}
</PRE>
<!-- END CODE //-->
<P>The following code returns the groups of the system in an array of groups. We note that the number of groups is increased by 1 so that the root group can be added to the end of the array:
</P>
<!-- CODE //-->
<PRE>
1. public ThreadGroup[] getThreadGroupsArray() {
2. ThreadGroup systemThreadGroup =
getSystemThreadGroup();
3. int numberOfGroups =
systemThreadGroup.activeGroupCount() +1;
4. ThreadGroup threadGroupsArray[] = new
5. ThreadGroup[numberOfGroups];
6. systemThreadGroup.enumerate(threadGroupsArray);
7. threadGroupsArray[numberOfGroups] =
systemThreadGroup;
8. return threadGroupsArray;
9. }
</PRE>
<!-- END CODE //-->
<P>When <I>group_instance.enumerate(ThreadGroup list[])</I> is called, all the thread group instances and their descendants are placed into the list. The list does not include the <I>group_instance</I> itself. This is why it is added to the end of the array on line 7.</P>
<P>Another form of <I>enumerate</I> is associated with a thread instance. <I>group_instance.enumerate(Thread list{})</I> returns an array of threads in this thread group and all descendant thread groups.</P>
<!-- CODE //-->
<PRE>
java.lang.ThreadGroup[name=system,maxpri=10]
Thread[Finalizer thread,1,system]
Thread[Idle thread,1,system]
java.lang.ThreadGroup[name=main,maxpri=10]
Thread[main,5,main]
Thread[Thread-2,5,main]
Thread[AWT-macos,5,main]
Thread[Thread-3,5,main]
Thread[Image Fetcher 0,8,main]
Thread[Image Fetcher 1,8,main]
Thread[Image Fetcher 2,8,main]
Thread[Image Fetcher 3,8,main]
Thread[Screen Updater,4,main]
Thread[Audio Player,10,main]
</PRE>
<!-- END CODE //-->
<P>The entire thread, called <I>RaceThread</I> (the new and improved version), follows:</P>
<!-- CODE //-->
<PRE>
class RaceThread extends Thread {
public void run() {
while (true) {
printThreadGroups();
try {Thread.sleep(10000);}
catch (InterruptedException e) {}
}
}
public ThreadGroup getSystemThreadGroup() {
ThreadGroup systemThreadGroup;
ThreadGroup parentThreadGroup;
systemThreadGroup = Thread.currentThread().getThreadGroup();
while ( ( parentThreadGroup = systemThreadGroup.getParent())
!= null)
systemThreadGroup = parentThreadGroup;
return systemThreadGroup;
}
public ThreadGroup[] getThreadGroupsArray() {
ThreadGroup systemThreadGroup = getSystemThreadGroup();
int numberOfGroups =
systemThreadGroup.activeGroupCount() +1;
ThreadGroup threadGroupsArray[] = new
ThreadGroup[numberOfGroups];
systemThreadGroup.enumerate(threadGroupsArray);
threadGroupsArray[numberOfGroups] = systemThreadGroup;
return threadGroupsArray;
}
public Thread [] getThreadsArray() {
ThreadGroup systemThreadGroup = getSystemThreadGroup();
Thread threadsArray[]= new
Thread[systemThreadGroup.activeCount()];
systemThreadGroup.enumerate(threadsArray);
return threadsArray ;
}
public void printThreadGroups() {
getSystemThreadGroup().list();
}
public void printThreads() {
Thread [] threadsArray = getThreadsArray();
for (int i = 0; i < threadsArray.length; i++)
System.out.println(threadsArray[i]);
}
public void printThreadsAndGroups() {
System.out.println(“The threads are:”);
printThreads();
System.out.println(“The groups are:”);
printThreadGroups();
}
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="097-103.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="107-108.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/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -