Java Reference
In-Depth Information
In the
main()
method you still create a
Thread
object for each thread of execution, but this time you use
a constructor that accepts an object of type
Runnable
as an argument. You pass an object of our class
JumbleNames
to it. This is possible because the
JumbleNames
class implements
Runnable
.
Thread Names
Threads have a name, which in the case of the
Thread
constructor you're using in the example is a default
name composed of the string
"Thread*"
with a sequence number appended. If you want to choose your
own name for a thread, you can use a
Thread
constructor that accepts a
String
object specifying the name
you want to assign to the thread. For example, you could have created the
Thread
object
first
with the
following statement:
Thread first = new Thread(new JumbleNames("Hopalong ", "Cassidy ", 200L),
"firstThread");
This assigns the name
"firstThread"
to the thread. Note that this name is used only when displaying
information about the thread. It has no relation to the identifier for the
Thread
object, and there's nothing,
apart from common sense, to prevent several threads being given the same name.
You can obtain the name assigned to a thread by calling the
getName()
method for the
Thread
object.
The name of the thread is returned as a
String
object. You can also change the name of a thread by calling
the
setName()
method defined in the class
Thread
and passing a
String
object to it.
After you've created the three
Thread
objects in the example, you call the
setDaemon()
method for each
of them. The rest of
main()
is the same as in the original version of the previous example, and you should
get similar output when you run this version of the program.
An object of a class that implements
Runnable
might need access to the
Thread
object that encapsulates
the thread in which it is executing. The static
currentThread()
method in the
Thread
class returns a refer-
ence to the current thread so that provides access when you need it.
MANAGING THREADS
In all the examples you've seen so far in this chapter, the threads are launched and then left to compete for
computer resources. Because all three threads compete in an uncontrolled way for a processor, the output
from the threads gets muddled. This isn't normally a desirable feature in a program. In most instances where
you use threads, you need to manage the way in which they execute so that their activities are coordinated
and they don't interfere with each other.
Of course, in our examples, the programs are deliberately constructed to release control of the processor
part way through outputting a name. While this is very artificial, similar situations can arise in practice, par-
ticularly where threads are involved in a repetitive operation. It is important to appreciate that a thread can
be interrupted while a source statement is executing. For example, imagine that a bank teller is crediting a
check to an account and at the same time the customer with that account is withdrawing some cash through
an ATM. This might happen in the following way:
• The bank teller checks the balance of the customer's account, which is $500.
• The ATM asks for the account balance.