|
Threads
An Introduction with Examples in Java |
|
Prof. David Bernstein
|
| Computer Science Department |
| bernstdh@jmu.edu |
RealTimeDispatchHandler does not start
working until the DailyDispatchHandler
has completed its job
while loop
in the processDispatches() method
maintains control of the CPU until all
of the daily dispatches have been completedDispatcher
"at the same time"
Dispatcher now has shared mutable state
DailyDispatchHandler uses a tight loop
that wastes processor resources
interrupt() Method:
true
isInterrupted() method of the
controlling Thread object
DailyDispatchHandler and
RealTimeDispatcher run until EOS
boolean attribute
called keepRunning
and change the do-while loop
from do{...}while (line != null); to
do{...}while (keepRunning && (line != null));
stop() method that assigns
false to keepRunning
keepRunning
(i.e., the thread that stop() executes in)
and another "loads" values from it (i.e., the dispatch handler's
thread)
volatile Modifier:
volatible attribute always returns the
most recent write by any threadvolatile reference types only provide this guarantee
for the referenec itself (e.g., not the elemnts of an array or
the attributes of an object)
Dispatcher has shared mutable state
(availableVehicles.size() > 0)
and then runs out of time
synchronized method
or block if it can acquire the relevant monitor
synchronized Methods:
synchronized
synchronized Blocks:
synchronized
Object
whose monitor should be used
Dispatcher
dispatch() method and the
makeVehicleAvailable()
method change the queue
makeVehicleAvailable()
Dispatcher
dispatch() method is modified so
that, instead of returning false if
no vehicles are available it loops until one is availabledispatch()
makeVehicleAvailable()
dispatch() method
enter the waiting state if no vehicles are
availablemakeVehicleAvailable() can be executed in
another threadmakeVehicleAvailable() must notify the waiting
threads
keepRunning before it was declared to be
volatile)
start() on a thread happens-before
any action in the started threadjoin() on that
thread