Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Similar documents
CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer.

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

CMSC 132: Object-Oriented Programming II. Threads in Java

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

Multi-threaded programming in Java

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

CMSC 330: Organization of Programming Languages. Concurrency & Multiprocessing

wait with priority An enhanced version of the wait operation accepts an optional priority argument:

Java Threads. COMP 585 Noteset #2 1

Threading and Synchronization. Fahd Albinali

Threads Questions Important Questions

7. MULTITHREDED PROGRAMMING

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS533 Concepts of Operating Systems. Jonathan Walpole

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Introduction to Java Threads

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

Concurrent Programming using Threads

Synchronising Threads

G52CON: Concepts of Concurrency

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

The New Java Technology Memory Model

Lecture 8: September 30

Synchronization I. Jo, Heeseung

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

Concurrency, Thread. Dongkun Shin, SKKU

CMSC 132: Object-Oriented Programming II

Advanced Concepts of Programming

CS3733: Operating Systems

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

Threads and Java Memory Model

Part I: Communication and Networking

Question Points Score Total 100

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Chapter 32 Multithreading and Parallel Programming

Threads Chate Patanothai

Multi-threading in Java. Jeff HUANG

CS 351 Design of Large Programs Threads and Concurrency

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Lesson 6: Process Synchronization

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (II)

CMSC 433 Programming Language Technologies and Paradigms. Synchronization

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

CS 10: Problem solving via Object Oriented Programming Winter 2017

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

CS11 Java. Fall Lecture 7

Chapter 6: Process Synchronization

Java Threads. Written by John Bell for CS 342, Spring 2018

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

Programming in Parallel COMP755

Concurrency Control. Synchronization. Brief Preview of Scheduling. Motivating Example. Motivating Example (Cont d) Interleaved Schedules

CS333 Intro to Operating Systems. Jonathan Walpole

THREADS & CONCURRENCY

W4118 Operating Systems. Instructor: Junfeng Yang

Review: Easy Piece 1

Object Oriented Programming. Week 10 Part 1 Threads

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

Performance Throughput Utilization of system resources

Threads, Concurrency, and Parallelism

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Threads and Parallelism in Java

Multitasking. Multitasking allows several activities to occur concurrently on the computer Levels of multitasking: Process based multitasking

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

Synchronization Principles

Concurrent Programming Lecture 10

CS510 Operating System Foundations. Jonathan Walpole

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

Dealing with Issues for Interprocess Communication

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

Java Threads. Introduction to Java Threads

Concurrent Programming

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Chapter 6: Process Synchronization

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Synchronization in Java

Operating Systems. Synchronization

CS 159: Parallel Processing

Concurrency in Java Prof. Stephen A. Edwards

THREADS AND CONCURRENCY

The University of Texas at Arlington

Transcription:

CMSC 330: Organization of Programming Languages Multithreaded Programming Patterns in Java CMSC 330 2 Multiprocessors Description Multiple processing units (multiprocessor) From single microprocessor to large compute clusters Can perform multiple tasks in parallel simultaneously Intel Core 2 Quad 6600 32 processor Pentium Xeon 106K processor IBM BlueGene/L CMSC 330 3 Concurrency Important & pervasive topic in CS Currently covered in CMSC 132 object-oriented programming II Java threads, data races, synchronization CMSC 313 low level programming / computer systems C pthreads CMSC 411/430 architectures / compilers Instruction level parallelism CMSC 412 operating systems Concurrent processes CMSC 424 database design Concurrent transactions CMSC 433 programming language technologies Advanced synchronization CMSC 451 algorithms Parallel algorithms CMSC 330 4 Computation Abstractions t1 t2 t1 t3 t2 t1 p1 p2 p3 p4 CPU 1 CPU 2 t4 t5! #"$&% ' ( )*,+*-, Processes vs. Threads int x x int x x.0/ 12436545365871:91 56<=>/ 3?7= = x int x x @0</ 3=765A56<=>/ 3?7= = BDC E< C 9F=:G/ 12436545 CMSC 330 5 CMSC 330 6 1

So, What Is a Thread? Implementation View Conceptually Parallel computation occurring within a process Implementation view A program counter and a stack Heap and static area are shared among all threads All programs have at least one thread (main) stack pointer Per-thread stack and instruction pointer Saved in memory when thread suspended Put in hardware / when thread resumes program counter CMSC 330 7 CMSC 330 8 Programming Threads Thread creation is inexpensive Threads reside on same physical processor Threads share memory, resources Except for local thread variables Shared-memory programming paradigm Threads communicate via shared data Synchronization used to avoid data races Limited scalability (10 s of threads) Programming Processes Process creation is expensive Request to operating system Processes may reside on separate processors Processes do not share memory Message-passing programming paradigm Messages using I/O streams, sockets, network, files Processes must cooperate to communicate Actions performed to send and receive data Highly scalable (1000 s of processors) CMSC 330 9 CMSC 330 10 Programming Languages & Threads Threads are available in many languages C, C++, Java, Ruby, OCaml In older languages (e.g., C and C++), threads are a platform specific add-on Not part of the language specification Implemented as code libraries (e.g., pthreads) In newer languages (e.g., Java, Ruby), threads are part of the language specification Not dependent on operating system Can utilize special keywords, syntax CMSC 330 11 CMSC 330 12 2

Java Threads Review Thread class & Runnable interface Used to create / manipulate threads Run-time scheduler Preemptive / non-premptive Thread states (new, runnable, blocked, dead) Data race Concurrent accesses to same shared object Where at least one access is a write Result may change depending on thread schedule Very difficult to detect & correct Java Threads Review (cont.) Synchronization Locks ensure exclusive access Provides mutual exclusion Only 1 thread can obtain lock at a time Lock associated w/ every Java object Use synchronized keyword to acquire lock Code blocks synchronized (o) { // lock for Object o Methods synchronized foo( ) { // lock for this Thread blocks when trying to acquire locked lock Thread returns when lock is finally acquired May deadlock if threads try to acquire each other s lock CMSC 330 13 CMSC 330 14 Synchronization Example (Java 1.4) Thread Creation public class Example extends Thread { private static int cnt = 0 static Object value = new Object() public void run() { int y = cnt cnt = y + 1 Value, any Java object Acquires the lock associated w/ value only succeeds if not held by another thread, otherwise blocks Releases the lock execution (time) main thread thread starts thread join thread starts thread ends CMSC 330 15 CMSC 330 16 Creating Threads in Java 1 Thread Class Approach Extend Thread class and override run method Example public class MyT extends Thread { public void run( ) { // work for thread MyT t = new MyT( ) // create thread t.start( ) // begin running thread // thread executing in parallel t.join() // waits for thread to exit CMSC 330 17 Creating Threads in Java 2 Runnable Approach 1. Define class implementing Runnable interface public interface Runnable { public void run( ) 2. Put work to be performed in run( ) method 3. Create instance of the worker class 4. Create thread to run it Create Thread object Pass worker object to Thread constructor Or hand the worker instance to an executor Alternative methods for running threads CMSC 330 18 3

Creating Threads in Java 2 (cont.) Example public class MyT implements Runnable { public void run( ) { // work for thread Thread t = new Thread(new MyT( )) // create thread t.start() // begin running thread // thread executing in parallel t.join() // waits for thread to exit Passing Parameters to Threads run( ) doesn t take parameters We pass parameters to the new thread by storing them as private fields In the Runnable object CMSC 330 19 CMSC 330 20 CMSC 330 21 Producer / Consumer Problem Suppose we are communicating with a shared variable E.g., a fixed size buffer holding messages One thread produces input to the buffer One thread consumes data from the buffer Rules Producer can t add input to the buffer if it s full Consumer can t take input from the buffer if it s empty CMSC 330 22 Producer / Consumer Idea If buffer is partially full, producer or consumer can run producer If buffer is empty, only producer can run producer c b a If buffer is full, only consumer can run consumer e d c b a consumer Broken Producer/Consumer Example void produce(object o) { while (valueready) while (!valueready) Object o = value Threads wait with lock held no way to make progress CMSC 330 23 CMSC 330 24 4

Broken Producer/Consumer Example void produce(object o) { while (valueready) while (!valueready) Object o = value valueready accessed without a lock held data race CMSC 330 25 Inefficient Producer/Consumer Example Constantly acquiring / releasing lock busy wait void produce(object o) { boolean done = false while (!done) { if (!valueready) { done = true Object o = null boolean done = false while (!done) { if (valueready) { o = value done = true CMSC 330 26 Solving Producer / Consumer Problem Difficult to use locks directly Very hard to get right Problems often very subtle Proper approach use condition variables Definition: a set of threads associated w/ a lock Also known as a wait set Waiting for some condition to become true Allows threads to sleep while waiting to acquire lock Can wake up sleeping threads before releasing lock In Java 1.4 Each monitor has a single condition variable CMSC 330 27 CMSC 330 28 Wait and NotifyAll (Java 1.4) Use synchronize on object to get associated lock Object o o s lock o s wait set Objects also have an associated wait set Can be viewed as an implicit condition variable CMSC 330 29 Wait and NotifyAll (cont.) o.wait() Must hold lock associated with o Release that lock And no other locks Adds this thread to wait set for lock Blocks the thread o.notifyall() Must hold lock associated with o Resumes all threads on lock s wait set Those threads must reacquire lock before continuing This is part of the function you don t need to do it explicitly CMSC 330 30 5

Producer/Consumer Example void produce(object o) { while (valueready) wait() notifyall() while (!valueready) wait() Object o = value notifyall() Using Correctly wait( ) must be called in a while loop may not be met when await returns Some other thread may have awoken first And changed condition (e.g., consumed item in buffer) Avoid holding other locks when waiting only gives up lock on object you are waiting on Reduces possibility of deadlock CMSC 330 31 CMSC 330 32 Broken Producer/Consumer Example void produce(object o) { if (valueready) wait() notifyall() if (!valueready) wait() Object o = value notifyall() Illegal access if multiple producers or consumers CMSC 330 33 Aspects of Synchronization Atomicity Locking to obtain mutual exclusion What we most often think about Visibility Ensuring that changes to object fields made in one thread are seen in other threads Ordering Ensuring that you aren t surprised by the order in which statements are executed CMSC 330 34 Key Ideas Multiple threads can run simultaneously Either truly in parallel on a multiprocessor Or can be scheduled on a single processor A running thread can be pre-empted at any time Threads can share data In Java, only fields can be shared Need to prevent data races Rule of thumb 1: You must hold a lock when accessing shared data Rule of thumb 2: You must not release a lock until shared data is in a valid state Overuse use of synchronization can create deadlock Rule of thumb: No deadlock if only one lock CMSC 330 35 6