DOC PREVIEW
LETU COSC 2103 - Multi threading

This preview shows page 1-2-3-27-28-29 out of 29 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

MultithreadingIntroductionThread States: Life Cycle of a ThreadSlide 4Operating System View Of runnable StateSlide 6Thread Priorities and Thread SchedulingPriorities and SchedulingCreating and Executing ThreadsSlide 10Slide 11Slide 12Producers and ConsumersSynchronizationThread SynchronizationSlide 16Slide 17Producer/Consumer Relationship without SynchronizationSlide 19Slide 20Slide 21Producer/Consumer Relationship with SynchronizationUsing Thread MethodsCircular BufferSlide 25Slide 26Daemon ThreadsRunnable InterfaceSlide 29MultithreadingChapter 232Introduction•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 programs3Thread 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•runnab le state–A thread in this state is ___________ its task•waiting state–A thread ___________ to this state to wait for another thread to perform a task4Thread 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 task5Operating System View Of runnable State•ready state–____________ waiting for another thread–Waiting for the ______________ to assign the thread a processor6Operating 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 state7Thread Priorities and Thread Scheduling•Java thread priority–Priority in range ______________•Timeslicing–Each thread assigned time on the processor (called a quantum )–Keeps ______________ threads running8Priorities and SchedulingThread.MAX_PRIORITY9Creating 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 pool10Creating and Executing Threads•ExecutorService interface–______________________ of Executor that declares other methods for managing the life cycle of an E xecutor–Can be created using _______________ methods of class Executors–Method shutdown _______________ when tasks are completed11Creating and Executing Threads•Executors class–Method newFixedThreadPool creates a pool consisting of a __________________________ of threads–Method newCachedThreadPool creates a pool that creates new threads _____________________________12Creating 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.13Producers and Consumers•Producer–Generating_______________•Consumer–Receives and _________________the output14Synchronization•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 producer15Thread 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 interface16Thread 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 ___________________ interface17Thread Synchronization•Condition interface–Declares methods await, to make a thread wait, –____________________, to wake up a waiting thread, and –signalAll, to wake up all waiting threads18Producer/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 data19Producer/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() method20Producer/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()21Producer/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 corrupted22Producer/Consumer Relationship with Synchronization•Solution is to _________________ the producer and consumer objects•Figure 23.11


View Full Document

LETU COSC 2103 - Multi threading

Documents in this Course
Arrays

Arrays

16 pages

Templates

Templates

17 pages

Methods

Methods

22 pages

Methods

Methods

22 pages

Arrays

Arrays

11 pages

Load more
Download Multi threading
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Multi threading 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 2 2 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?