Multithreading Chapter 23. 2 Introduction Consider ability of _____________ to multitask...
-
Upload
brent-lang -
Category
Documents
-
view
214 -
download
2
Transcript of Multithreading Chapter 23. 2 Introduction Consider ability of _____________ to multitask...
Multithreading
Chapter 23
2
Introduction
• Consider ability of _____________ to multitask– Breathing, heartbeat, chew gum, walk …
• In many situations we need a computer to multitask
• Concurrency normally available in ________________________
• Java provides built-in _______________– Multithreading improves the performance of some
programs
3
Thread States: Life Cycle of a Thread
• _________ state– Thread was just created
• Ready state– Thread’s __________ method invoked– Thread can now execute
• Running state– Thread is _______ a processor and running
• _______________ state– Thread has completed or exited– Eventually disposed of by system
View diagram, Figure 16.1
4
Thread Priorities and Thread Scheduling
• Java thread priority– Priority in range ___________
• Timeslicing– Each thread assigned time on the processor
(called a _________________)– Keeps highest priority threads running
5
Priorities and
Scheduling
Priority 9
Priority 8
Priority 7
Priority 10
Priority 6
Priority 5
Priority 4
Priority 3
Priority 2
Priority 1
A B
D
C
E F
G
H I
J K
Ready threads
Thread.MIN_PRIORITY
Thread.MAX_PRIORITY
Thread.NORM_PRIORITY
6
Creating and Executing Threads
• Figure 16.3
• Demonstrates– Constructing ____________ objects– Using Thread methods start and __________– Creates 3 equal priority threads– Each is put to sleep for random number of
milliseconds– When __________, it displays name, etc.
7
Thread Synchronization
• Java uses monitors for thread synchronization
• The sychronized keyword– Every synchronized method of an object has a
_________________– _____________ inside a synchronized method
at a time– All other threads ___________ until method finishes– Next highest priority thread runs when method
finishes
8
Producers and Consumers
• Producer– Generating
____________
• Consumer– ___________ and
processesthe output
9
Synchronization
• Problem– Sometimes the producer gets
too far ___________ of the consumer • The objects produced fill up the holding area (________)• The producer must wait for space to place objects
– Sometimes the consumer gets ahead of the producer
• There are no objects to be processed (______________)• The consumer must wait for the producer
10
Producer/Consumer Relationship without Synchronization
• Buffer– __________________ memory region
• Producer thread– Generates data to add to buffer– Calls _____________ if consumer has not read previous
message in buffer– Writes to _______ buffer and calls notify for consumer
• ________________ thread– Reads data from buffer– Calls wait if buffer empty
• _______________ threads to avoid corrupted data
11
Producer/Consumer Relationship without Synchronization
• View source code which establishes– Buffer, Figure 16.4
• An interface which specifies ______________ methods
– Producer, Figure 16.5• __________________ of Thread• Uses a shared Buffer object• Method run is overridden from Thread class• Uses Buffer.set() method
– Consumer, Figure 16.6• Also a subclass of Thread, also uses shared Buffer• Uses the _________________ method
12
Producer/Consumer Relationship without Synchronization
• View Figure 16.7 which implements the Buffer interface– Implements the get and set methods
• This ___________________________ object is used in Figure 16.8 program– Buffer object declared, instantiated– Also Producer and Consumer objects– _________________ call method start()
13
Producer/Consumer Relationship without Synchronization
• Example randomly called producer and consumer
• You should note that in some cases the data was _____________________ !!– Consumer reads values _________ producer
generates– Consumer misses a value– Consumer reads _____________ multiple times
• We need to deal with problem so data is not corrupted
14
Producer/Consumer Relationship with Synchronization
• Solution is to synchronize the producer and consumer objects
• Figure 16.9 implements a buffer and synchronizes – Consumer consumes only _______ produces a
value– ________________ produces a value only after
consumer consumes previous value produced– Condition variable occupiedBufferCount
determines __________________________
• Program which uses this, Figure 16.10
15
Circular Buffer• Features
– Multiple memory cells– Produce item if one or more _________ cells– ____________ item if one or more filled cells
• Caveats– Producer and consumers must be relatively
_________________________• Otherwise buffer fills up or stays empty
– Synchronization still necessary– Seek to __________________ buffer size
• minimizes thread-wait time
16
Circular Buffer
• Code in figures noted below set up circular buffer, producer, consumer– Figure 16.11
• Implements RunnableOutput class• Overrides run method
– Figure 16.12• Loads values in buffer• Instantiates a RunnableOutput object
– Figure 16.13• Instantiates RunnableOutput object
17
Circular Buffer
• Figure 16.14 CircularBuffer– Instantiates an ___________, the shared area– Methods get() and set() implemented
• Figure 16.15– Program to demonstrate use of circular buffer– __________________ Producer and Consumer
objects– Note that it is a windowed application
• Sends output to _____________________
18
Daemon Threads
• Run for benefit of other threads– Do not prevent program from ________________– Garbage collector is a daemon thread
• Set daemon thread with method setDaemon– Must be done at ____________________
• Do not assign ___________________ to daemon thread– Will be terminated without warning– May prevent those tasks from completing properly
19
Runnable Interface
• May be necessary to extend a class that already extends a class __________________
• Java does not allow a class to extend more than one class at a time– __________________________ for multithreading
support
• Program that uses a Runnable object to control a thread– Creates a __________________ object– Associates the Runnable object with that Thread
class
20
Runnable Interface
• Illustration of using a Runnable interface– Figure 16.16
• Note methods start, stop, run