Wednesday, October 14, 2009

Thread Constructors




















Chapter 3 - Creating and Starting a Thread

Java Thread Programming
Paul
Hyde






size=1>  size=2>Copyright � 1999 Sams
Publishing




















size=4>Thread Constructors












size=2>The central constructor in face="Courier New" size=2>Thread face=Arial size=2> is












face="Courier New" size=2>public Thread(ThreadGroup group, Runnable
target, String name)












size=2>The parameter size=2>name allows you
to specify the name of the thread at construction time, rather than
set it later using
size=2>setName() size=2>.












size=2>The parameter size=2>target refers to
an object of type
size=2>Runnable. This
object’s
size=2>run() method
will be invoked by the new thread instead of the
color=#010100 face="Courier New" size=2>run() color=#010100 face=Arial size=2> method of color=#010100 face="Courier New" size=2>Thread color=#010100 face=Arial size=2>. Unlike a thread’s name, if you’re
going to specify the
size=2>target, you must
do so at the time of construction.
color=#008000 face=Arial size=2>Chapter 4 color=#000000 face=Arial size=2> explores in detail the issues
involved in making a decision to use
face="Courier New" size=2>Runnable face=Arial size=2> instead of extending face="Courier New" size=2>Thread face=Arial size=2>.












size=2>The parameter size=2>group lets you
specify the
size=2>ThreadGroup to
which the new
size=2>Thread will
belong. Figure 3.1 shows the relationships among
color=#010100 face="Courier New" size=2>Thread color=#010100 face=Arial size=2>s and face="Courier New" size=2>ThreadGroup face=Arial size=2>s.












size=2>














size=2>Figure 3.1: size=2>A sample hierarchy of ThreadGroups and Threads.












size=2>An analogy can be drawn between the file system concept of
directories and files and the
face="Courier New" size=2>ThreadGroup face=Arial size=2>/ size=2>Thread
relationship. In a file system, a directory can contain files and
other directories. These other directories, in turn, can contain
other files and directories. Every file is in exactly one directory,
which may itself be in another directory. Every directory except the
one “root” or “base” directory is in another directory.












size=2>In Java, a size=2>ThreadGroup
(much like a directory) can contain
face="Courier New" size=2>Thread face=Arial size=2>s and other face="Courier New" size=2>ThreadGroup face=Arial size=2>s. These other face="Courier New" size=2>ThreadGroup face=Arial size=2>s can, in turn, contain other color=#010100 face="Courier New" size=2>Thread color=#010100 face=Arial size=2>s and face="Courier New" size=2>ThreadGroup face=Arial size=2>s. Every face="Courier New" size=2>Thread face=Arial size=2> (much like a file) is a member of a color=#010100 face="Courier New" size=2>ThreadGroup color=#010100 face=Arial size=2>, which may itself be a member of
another
size=2>ThreadGroup size=2>.












size=2>If a size=2>ThreadGroup is
not specified at the time of a
face="Courier New" size=2>Thread face=Arial size=2>’s construction, the face="Courier New" size=2>ThreadGroup face=Arial size=2> is inherited from the face="Courier New" size=2>Thread face=Arial size=2> that constructed it. color=#008000 face=Arial size=2>Chapter 10, “Thread
Groups,” discusses
size=2>ThreadGroups in
more detail.












size=2>The size=2>Thread
constructor used so far has been as follows:












face="Courier New" size=2>public Thread()












size=2>By default, the size=2>ThreadGroup is
that of the
size=2>Thread that
constructs it. No external
face="Courier New" size=2>Runnable face=Arial size=2> is specified, so the face="Courier New" size=2>Thread face=Arial size=2>’s own face="Courier New" size=2>run() size=2> method is called. Because no name is specified, the name of
the
size=2>Thread will be
automatically generated as something such as
color=#010100 face="Courier New" size=2>Thread-0 color=#010100 face=Arial size=2>.












size=2>The other constructors of face="Courier New" size=2>Thread face=Arial size=2> come somewhere in between the zero-argument
constructor and the three-argument constructor. They specify some
parameters of the three-argument constructor, and the nonspecified
parameters take on their default values.












valign="top" bgcolor="#FFFFFF">

Toc



No comments:

Post a Comment