LETU COSC 2103 - Multi threading Chapter 23 (29 pages)

Previewing pages 1, 2, 3, 27, 28, 29 of 29 page document View the full content.
View Full Document

Multi threading Chapter 23



Previewing pages 1, 2, 3, 27, 28, 29 of actual document.

View the full content.
View Full Document
View Full Document

Multi threading Chapter 23

93 views


Pages:
29
School:
LeTourneau University
Course:
Cosc 2103 - Computer Science II
Computer Science II Documents
Unformatted text preview:

Multithreading Chapter 23 Introduction Consider ability of human body to Breathing heartbeat chew gum walk In many situations we need a computer to multitask Concurrency normally available in Java provides built in multithreading Multithreading improves the of some programs 2 Thread States Life Cycle of a Thread 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 its task waiting state A thread to this state to wait for another thread to perform a task 3 Thread States Life Cycle of a Thread 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 state when it is signaled by another thread or when the timed interval expires terminated state A runnable thread enters this state when it its task 4 Operating System View Of runnable State ready state waiting for another thread Waiting for the to assign the thread a processor 5 Operating System View Of runnable State running state Currently has a and is executing Often executes for a small amount of processor time called a before transitioning back to the ready state 6 Thread Priorities and Thread Scheduling Java thread priority Priority in range Timeslicing Each thread assigned time on the processor called a quantum Keeps threads running 7 Thread MAX PRIORITY Priorities and Scheduling 8 Creating and Executing Threads Runnable interface Preferred means of creating a multithreaded application Declares method Executed by an object that implements the Executor interface Executor interface Declares method Creates and manages a group of threads called a thread pool 9 Creating and Executing Threads ExecutorService interface of Executor that declares other methods for managing the life cycle of an Executor Can be created using methods of class Executors Method shutdown when tasks are completed 10 Creating and Executing Threads Executors class Method newFixedThreadPool creates a pool consisting of a of threads Method newCachedThreadPool creates a pool that creates new threads 11 Creating and Executing Threads PrintTask class Figure 23 4 RunnableTester Figure 23 5 Demonstrates Thread objects Using Thread methods and sleep Creates 3 equal priority threads Each is put to sleep for random number of milliseconds When awakened it displays name etc 12 Producers and Consumers Producer Generating Consumer Receives and the output 13 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 gets ahead of the producer There are no objects to be processed buffer The consumer must wait for the producer 14 Thread Synchronization Thread synchronization Provided to the programmer with Exclusive access to a shared object Implemented in Java using Lock interface lock method obtains the lock enforcing mutual exclusion unlock method the lock Class ReentrantLock implements the Lock 15 interface 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 Create by calling Lock method newCondition Represented by an object that implements the interface 16 Thread Synchronization Condition interface Declares methods await to make a thread wait to wake up a waiting thread and signalAll to wake up all waiting threads 17 Producer Consumer Relationship without Synchronization Buffer memory region Producer thread Generates to add to buffer Calls wait if consumer has not read previous message in buffer Writes to empty buffer and calls for consumer Consumer thread Reads data from buffer Calls wait if buffer Synchronize threads to avoid corrupted data 18 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 of Thread Uses a shared Buffer object Method run is 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 19 Producer Consumer Relationship without Synchronization View Figure 23 9 which implements the Buffer interface Implements the methods This UnsynchronizedBuffer object is used in Figure 23 10 program Buffer object declared instantiated Also Producer and Consumer objects Both threads call method start 20 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 a value Consumer reads same value multiple times We need to deal with problem so data is not 21 corrupted Producer Consumer Relationship with Synchronization Solution is to the producer and consumer objects Figure 23 11 implements a buffer and synchronizes Consumer consumes only produces a value Producer produces a value only after consumer consumes value produced Condition variable occupiedBufferCount determines whose turn it is Program which uses this Figure 23 12 22 Using Thread Methods Create Wait class Has methods Provide capability to have another application for a certain amount of time Note Example to act as a simple timer 23 Circular Buffer Features 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 speed Otherwise buffer fills up or stays empty Synchronization still necessary Seek to optimize buffer size thread wait time 24 Circular Buffer Circular Buffer class Figure 23 13 for mutual exclusion Condition variables to control writing and reading Circular buffer provides three spaces for data Obtain the lock writing data to the circular buffer Wait until a buffer space is Impose circularity of the buffer waiting thread when to read data from buffer Release lock 25 Circular Buffer Circular Buffer test Figure 23 14 Create instance of circular buffer Execute producer consumer in separate threads 26 Daemon Threads Run for benefit of Do not prevent program from terminating is a daemon thread Set daemon thread with method setDaemon Must be done at time Do not assign tasks to daemon thread Will be terminated without warning May prevent those tasks from completing properly 27 Runnable Interface May be necessary to a class that already extends


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Multi threading Chapter 23 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Multi threading Chapter 23 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?