Download presentation
Presentation is loading. Please wait.
Published byBrent Lang Modified over 9 years ago
1
Multithreading Chapter 23
2
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
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
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
5 Priorities and Scheduling Priority 9 Priority 8 Priority 7 Priority 10 Priority 6 Priority 5 Priority 4 Priority 3 Priority 2 Priority 1 AB D C EF G H I JK Ready threads Thread.MIN_PRIORITY Thread.MAX_PRIORITY Thread.NORM_PRIORITY
6
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
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
8 Producers and Consumers Producer –Generating ____________ Consumer –___________ and processes the output
9
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
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
11 Producer/Consumer Relationship without Synchronization View source code which establishes –Buffer, Figure 16.4Figure 16.4 An interface which specifies ______________ methods –Producer, Figure 16.5Figure 16.5 __________________ of Thread Uses a shared Buffer object Method run is overridden from Thread class Uses Buffer.set() method –Consumer, Figure 16.6Figure 16.6 Also a subclass of Thread, also uses shared Buffer Uses the _________________ method
12
12 Producer/Consumer Relationship without Synchronization View Figure 16.7 which implements the Buffer interfaceFigure 16.7 –Implements the get and set methods This ___________________________ object is used in Figure 16.8 programFigure 16.8 –Buffer object declared, instantiated –Also Producer and Consumer objects –_________________ call method start()
13
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
14 Producer/Consumer Relationship with Synchronization Solution is to synchronize the producer and consumer objects Figure 16.9 implements a buffer and synchronizesFigure 16.9 –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.10Figure 16.10
15
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
16 Circular Buffer Code in figures noted below set up circular buffer, producer, consumer –Figure 16.11Figure 16.11 Implements RunnableOutput class Overrides run method –Figure 16.12Figure 16.12 Loads values in buffer Instantiates a RunnableOutput object –Figure 16.13Figure 16.13 Instantiates RunnableOutput object
17
17 Circular Buffer Figure 16.14 CircularBufferFigure 16.14 –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
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
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
20 Runnable Interface Illustration of using a Runnable interface –Figure 16.16Figure 16.16 Note methods start, stop, run
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.