1 Ivan Marsic Rutgers University LECTURE 19: Concurrent Programming.

Slides:



Advertisements
Similar presentations
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
1 Race Conditions When threads share access to a common object/data, they can conflict with each other and mess up the consistency of the object/data.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
111 © 2002, Cisco Systems, Inc. All rights reserved.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
Internet Software Development Controlling Threads Paul J Krause.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Department of Computer Science and Software Engineering
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Multi-Threading in Java
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CMSC 330: Organization of Programming Languages Threads.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
1 OS Review Processes and Threads Chi Zhang
C H A P T E R E L E V E N Concurrent Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
T HREADS Lecture 5 1 L. Mohammad R.Alkafagee. H EAVYWEIGHT - PROCESSES The cooperation of traditional processes also known as heavyweight processes which.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Multithreaded Programming in Java David Meredith Aalborg University.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
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.
Tutorial 2: Homework 1 and Project 1
Multithreading / Concurrency
Multithreaded Programming in Java
Lecture 21 Concurrency Introduction
143a discussion session week 3
Multithreading.
Multithreading.
Concurrency: Mutual Exclusion and Process Synchronization
21 Threads.
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
NETWORK PROGRAMMING CNET 441
CS333 Intro to Operating Systems
Threads and Multithreading
CS703 – Advanced Operating Systems
Multithreaded Programming in Java
Chapter 3: Process Management
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

1 Ivan Marsic Rutgers University LECTURE 19: Concurrent Programming

2 Topics  Threads  Exclusive Resource Access –Exclusion Synchronization  Cooperation between Threads –Condition Synchronization  Concurrent Programming Example

3 Parallelism  What we want: Parallel instead of serial processing, to speed up service  What problems to solve: Concurrent access to a resource (or software object) can lead to conflict  ambiguous result or “frozen” program

4 Concurrency: Exclusion Synchronization

5 Thread vs. Process  Process roughly corresponds to a program (* program can “spawn” many processes )  Processes communicate using inter- process communication (pipes, sockets, shared memory, …) –An object from one process cannot directly call a method on an object shared by another process  Threads run in the same program –An object from one thread can directly call a method on an object shared by another thread

6 Concurrent Programming -- Threads Lifecycle of Java threads

7 Thread States: Alive  Alive: After a thread is start() -ed, it becomes alive: –Runnable: The thread can be run when the OS scheduler can arrange it (and nothing prevents it from being run) –Blocked: The thread could be run, but there is something that prevents it (e.g., another thread is holding the resource needed for this thread to do its work). While a thread is in the blocked state, the scheduler will simply skip over it and not give it any CPU time. A thread can become blocked for the following reasons: Waiting for notification: Invoking the method wait() suspends the thread until the thread gets the notify() or notifyAll() message Waiting for I/O or lock: The thread is waiting for an input or output operation to complete, or it is trying to call a synchronized method on a shared object, and that object’s lock is not available Waiting for rendezvous: Invoking the method join(target) suspends the thread until the target thread returns from its run() method Sleeping: Invoking the method sleep(milliseconds) suspends the thread for the specified time

8 Exclusion Synchronization

9 Example: Bank Account Access by Two Users  Concurrent read/write of the same data by several threads  “race condition” or “race hazard”  The outcome of the execution depends on the particular order in which the access takes place Thread 1 Thread 2 oldBalance = account.getBalance();... newBalance = oldBalance + deposit; oldBalance = account.getBalance(); account.setBalance(newBalance); newBalance = oldBalance - withdrawal;... account.setBalance(newBalance);

10 Exclusion Synchronization in Java public class SharedClass {... public synchronized void method1(... ) {... } acquire lock release lock shared object public class AnyClass {... public void method2(... ) {... synchronized (expression) { statement }... } acquire lock release lock shared object Synchronized MethodsSynchronized Statements (a)(b)

11 Condition Synchronization suspend and wait … … resume the suspended work

12 Example: Safe Home Access Backyard door: Access lock Front door: Access lock Central Computer

13 Example of Concurrency Gains Single thread – sequential service (a) Multiple threads – parallel service (b)

14 Where Latency Matters?  While typing in the digits, the user does not notice latency  Latency becomes noticeable when waiting for validity check and device activation  Validity computation is quick; device activation may be somewhat slower  The longest latency is while the user is typing-in the keycode so we need to allow parallel entry of keycodes on both doors  But...all communication goes over the same serial cable: Tenant Read Digits [ keycode valid ] [else] Check Validity Activate Devices [ keycode complete ] [else]

15 Hardware Implementation NOTE: Locks, lightbulb switch (with photosensor) and alarm bell are controlled by the central computer via the same serial port

16 Multithreaded Implementation keyFront : StringBufferkeyBack : StringBuffer contrlBack : ControllerThdcontrlFront : ControllerThd HomeAccessControlSystem_2x + serialEvent(event : SerialPortEvent) Main Thread: interacts with serial I/O port Helper Thread: back door calculations and control Helper Thread: front door calculations and control Shared Object ( See Listing 5-6 in the book for details )