Inherit
a class from
Thread
and override the
run( )
method. Inside
run( ),
print a message, then call
sleep( ).
Repeat this three times, then return from
run( ).
Put a start-up message in the constructor and override
finalize( )
to print a shut-down message. Make a separate thread class that calls
System.gc( )
and
System.runFinalization( )
inside
run( ),
printing a message as it does so. Make several thread objects of both types and
run them to see what happens.
Modify
Counter2.java
so that the thread is an inner class and doesn’t need to explicitly store
a handle to a
Counter2.
Modify
Sharing2.java
to add a
synchronized
block inside the
run( )
method of
TwoCounter
instead of synchronizing the entire
run( )
method.
Create
two
Thread
subclasses, one with a
run( )
that starts up, captures the handle of the second
Thread
object and then calls
wait( ).
The other class’
run( )
should call
notifyAll( )
for the first thread after some number of seconds have passed, so the first
thread can print out a message.
In
Counter5.java
inside
Ticker2,
remove the
yield( )
and explain the results. Replace the
yield( )
with a
sleep( )
and explain the results.
In
ThreadGroup1.java,
replace the call to
sys.suspend( )
with a call to
wait( )
for the thread group, causing it to wait for two seconds. For this to work
correctly you must acquire the lock for
sys
inside
a
synchronized
block.
Change
Daemons.java
so that
main( )
has a
sleep( )
instead of a
readLine( ).
Experiment with different sleep times to see what happens.
(Intermediate)
In Chapter 7, locate the
GreenhouseControls.java
example, which consists of three files. In
Event.java,
the class
Event
is based on watching the time. Change
Event
so that it is a
Thread,
and change the rest of the design so that it works with this new
Thread-based
Event.