1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.

Slides:



Advertisements
Similar presentations
Practical Session 6 Multitasking vs. multithreading Threads Concurrency vs. Parallelism Java Threads Thread confinement Object/Class Immutability.
Advertisements

Chapter 7 - Resource Access Protocols (Critical Sections) Protocols: No Preemptions During Critical Sections Once a job enters a critical section, it cannot.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
 2005 Pearson Education, Inc. All rights reserved Multithreading.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Chapter 8 Three Interfaces: Cloneable, Serializable, and Runnable.
 2007 Pearson Education, Inc. All rights reserved. 1 Ch23 Multithreading: OBJECTIVES In this chapter you will learn:  What threads are and why they are.
1 CS2200 Software Development Lecture: Multi-threading II A. O’Riordan, 2009.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
 2007 Pearson Education, Inc. All rights reserved Multithreading.
A Introduction to Computing II Lecture 18: Concurrency Issues Fall Session 2000.
 2006 Pearson Education, Inc. All rights reserved Multithreading.
1 L50 Multithreading (2). 2 OBJECTIVES  What producer/consumer relationships are and how they are implemented with multithreading.
Chapter 6 – Concurrent Programming Outline 6.1 Introduction 6.2Monitors 6.2.1Condition Variables 6.2.2Simple Resource Allocation with Monitors 6.2.3Monitor.
1 Uniprocessor Scheduling Chapter 9. 2 Aims of Scheduling Assign processes to be executed by the processor(s) Response time Throughput Processor efficiency.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
1 Java Threads Instructor: Mainak Chaudhuri
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Java Threads Representation and Management of Data on the Internet.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
Synchronizing threads, thread pools, etc.
Uniprocessor Scheduling
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
Multithreading in JAVA
Scis.regis.edu ● CS-434: Object-Oriented Programming Using Java Week 8 Dr. Jesús Borrego Adjunct Faculty Regis University 1.
Java Thread and Memory Model
Threading and Concurrency COM379T John Murray –
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
THREAD MODEL.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Multithreading (Based on Chapter 29 (Multithreading) Liang book and Chapter 5 (Threads) Elliotte Rusty book) Dr. Tatiana Balikhina 1.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Chapter 4 – Thread Concepts
Multithreading / Concurrency
Multithreading Lec 23.
Multithreading.
Chapter 4 – Thread Concepts
Multithreaded Programming in Java
Lecture 21 Concurrency Introduction
23 Multithreading.
Multithreading Chapter 23.
Multithreading.
TDC 311 Process Scheduling.
Multithreading.
Multithreaded Programming
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
Multithreading in java.
Threads and Multithreading
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

1 L49 Multithreading (1)

2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life cycle of a thread.  Thread priorities and scheduling.  To create and execute Runnables.  Thread synchronization.

Introduction Multithreading – Provides application with multiple threads of execution – Allows programs to perform tasks concurrently – Often requires programmer to synchronize threads to function correctly

Thread States: Life Cycle of a Thread Thread states – 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

Thread States: Life Cycle of a Thread Thread states – 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 Fig | Thread life-cycle UML state diagram.

Thread States: Life Cycle of a Thread Operating system view of runnable state – ready state A thread in this state is not waiting for another thread, but is waiting for the operating system to assign the thread a processor – running state A thread in this state currently has a processor and is executing – A thread in the running state often executes for a small amount of processor time called a time slice or quantum before transitioning back to the ready state

8 Fig | Operating system’s internal view of Java’s runnable state.

Thread Priorities and Thread Scheduling Priorities – Every Java thread has a priority – Java priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10) – Threads with a higher priority are more important and will be allocated a processor before threads with a lower priority – Default priority is NORM_PRIORITY (a constant of 5)

Thread Priorities and Thread Scheduling Thread scheduler – Determine which thread runs next – Simple implementation runs equal-priority threads in a round-robin fashion – Higher-priority threads can preempt the currently running thread – In some cases, higher-priority threads can indefinitely postpone lower-priority threads which is also known as starvation

11 Fig | Thread-priority scheduling.

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

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 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

14 Outline PrintTask.java (1 of 2) Implement runnable to create separate thread

15 Outline PrintTask.java (2 of 2) Declare run method to fulfill interface

16 Outline RunnableTester.java (1 of 2) Create three PrintTask s; each will run in a separate thread Create fixed thread pool to execute and manage threads Execute each task; this method will assign a thread to the runnable Shutdown thread pool when runnable s complete their tasks

17 Outline RunnableTester.java (2 of 2)

Thread Synchronization 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

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 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