Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multithreading Chapter 23.

Similar presentations


Presentation on theme: "Multithreading Chapter 23."— Presentation transcript:

1 Multithreading Chapter 23

2 What You Will Learn Thread States Thread Synchronization
Producers and consumers

3 Introduction Consider ability of human body to multitask
Breathing, heartbeat, chew gum, walk … In many situations we need a computer to multitask Concurrency normally available in OS primitives Java provides built-in multithreading Multithreading improves the performance of some programs

4 Thread States: Life Cycle of a Thread
View diagram, Figure 23.1 new state New thread begins its life cycle in the new state Remains in this state until program starts the thread, placing it in the runnable state runnable state A thread in this state is executing its task waiting state A thread transitions to this state to wait for another thread to perform a task

5 Thread States: Life Cycle of a Thread
View diagram, Figure 23.1 timed waiting state A thread enters this state to wait for another thread or for an amount of time to elapse A thread in this state returns to the runnable state when it is signaled by another thread or when the timed interval expires terminated state A runnable thread enters this state when it completes its task

6 Operating System View Of runnable State
ready state Not waiting for another thread Waiting for the operating system to assign the thread a processor

7 Operating System View Of runnable State
running state Currently has a processor and is executing Often executes for a small amount of processor time called a time slice or quantum before transitioning back to the ready state

8 Thread Priorities and Thread Scheduling
Java thread priority Priority in range 1-10 Timeslicing Each thread assigned time on the processor (called a quantum) Keeps highest priority threads running

9 Priorities and Scheduling
Thread.MAX_PRIORITY Priorities and Scheduling

10 Creating and Executing Threads
Runnable interface Preferred means of creating a multithreaded application Declares method run Executed by an object that implements the Executor interface Executor interface Declares method execute Creates and manages a group of threads called a thread pool

11 Creating and Executing Threads
ExecutorService interface Subinterface of Executor that declares other methods for managing the life cycle of an Executor Can be created using static methods of class Executors Method shutdown ends threads when tasks are completed

12 Creating and Executing Threads
Executors class Method newFixedThreadPool creates a pool consisting of a fixed number of threads Method newCachedThreadPool creates a pool that creates new threads as they are needed

13 Creating and Executing Threads
PrintTask class Figure 23.4 RunnableTester, Figure 23.5 Demonstrates Constructing Thread objects Using Thread methods start and sleep Creates 3 equal priority threads Each is put to sleep for random number of milliseconds When awakened, it displays name, etc.

14 Producers and Consumers
Generating output Consumer Receives and processes the output

15 Synchronization Problem
Sometimes the producer gets too far ahead of the consumer The objects produced fill up the holding area (buffer) The producer must wait for space to place objects Sometimes the consumer gets ahead of the producer There are no objects to be processed (empty buffer) The consumer must wait for the producer

16 Thread Synchronization
Provided to the programmer with mutual exclusion Exclusive access to a shared object Implemented in Java using locks Lock interface lock method obtains the lock, enforcing mutual exclusion unlock method releases the lock Class ReentrantLock implements the Lock interface

17 Thread Synchronization
Condition variables If a thread holding the lock cannot continue with its task until a condition is satisfied, the thread can wait on a condition variable Create by calling Lock method newCondition Represented by an object that implements the Condition interface

18 Thread Synchronization
Condition interface Declares methods await, to make a thread wait, signal, to wake up a waiting thread, and signalAll, to wake up all waiting threads

19 Producer/Consumer Relationship without Synchronization
Buffer Shared memory region Producer thread Generates data to add to buffer Calls wait if consumer has not read previous message in buffer Writes to empty buffer and calls notify for consumer Consumer thread Reads data from buffer Calls wait if buffer empty Synchronize threads to avoid corrupted data

20 Producer/Consumer Relationship without Synchronization
View source code which establishes Buffer, Figure 23.6 An interface which specifies get and set methods Producer, Figure 23.7 Subclass of Thread Uses a shared Buffer object Method run is overridden from Thread class Uses Buffer.set() method Consumer, Figure 23.8 Also a subclass of Thread, also uses shared Buffer Uses the Buffer.get() method

21 Producer/Consumer Relationship without Synchronization
View Figure which implements the Buffer interface Implements the get and set methods This UnsynchronizedBuffer object is used in Figure program Buffer object declared, instantiated Also Producer and Consumer objects Both threads call method start()

22 Producer/Consumer Relationship without Synchronization
Example randomly called producer and consumer You should note that in some cases the data was incorrect Consumer reads values before producer generates Consumer misses a value Consumer reads same value multiple times We need to deal with problem so data is not corrupted

23 Producer/Consumer Relationship with Synchronization
Solution is to synchronize the producer and consumer objects Figure implements a buffer and synchronizes Consumer consumes only after produces a value Producer produces a value only after consumer consumes previous value produced Condition variable occupiedBufferCount determines whose turn it is Program which uses this, Figure 23.12

24 Using Thread Methods Create Wait class Has static methods
Provide capability to have another application pause for a certain amount of time Note Example to act as a simple timer.

25 Circular Buffer Features Caveats Multiple memory cells
Produce item if one or more empty cells Consume item if one or more filled cells Caveats Producer and consumers must be relatively same speed Otherwise buffer fills up or stays empty Synchronization still necessary Seek to optimize buffer size minimizes thread-wait time

26 Circular Buffer Circular Buffer class Figure 23.13
Lock for mutual exclusion Condition variables to control writing and reading Circular buffer; provides three spaces for data Obtain the lock before writing data to the circular buffer Wait until a buffer space is empty Impose circularity of the buffer Signal waiting thread when to read data from buffer Release lock

27 Circular Buffer Circular Buffer test, Figure 23.14
Create instance of circular buffer Execute producer, consumer in separate threads

28 Daemon Threads Run for benefit of other threads
Do not prevent program from terminating Garbage collector is a daemon thread Set daemon thread with method setDaemon Must be done at start time Do not assign critical tasks to daemon thread Will be terminated without warning May prevent those tasks from completing properly

29 Runnable Interface May be necessary to extend a class that already extends a class other than Thread Java does not allow a class to extend more than one class at a time Implement Runnable for multithreading support Program that uses a Runnable object to control a thread Creates a Thread object Associates the Runnable object with that Thread class

30 Runnable Interface Illustration of using a Runnable interface
Figure 23.18 Note methods start, stop, run


Download ppt "Multithreading Chapter 23."

Similar presentations


Ads by Google