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

Similar documents
CMSC 433 Programming Language Technologies and Paradigms. Concurrency

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

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

CMSC 330: Organization of Programming Languages

Object Oriented Programming. Week 10 Part 1 Threads

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

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

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1)

Concurrent Programming using Threads

CMSC 330: Organization of Programming Languages. Concurrency & Multiprocessing

CMSC 330: Organization of Programming Languages

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Multi-threading in Java. Jeff HUANG

Threads Chate Patanothai

Animation Part 2: MoveableShape interface & Multithreading

Threads and Parallelism in Java

CMSC 132: Object-Oriented Programming II

7. MULTITHREDED PROGRAMMING

Threads Questions Important Questions

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

Unit 4. Thread class & Runnable Interface. Inter Thread Communication

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

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

Unit III Rupali Sherekar 2017

CIS233J Java Programming II. Threads

CS 351 Design of Large Programs Threads and Concurrency

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

Unit - IV Multi-Threading

Multithread Computing

Multiple Inheritance. Computer object can be viewed as

Note: Each loop has 5 iterations in the ThreeLoopTest program.

Java s Implementation of Concurrency, and how to use it in our applications.

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

Introduction to Java Threads

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Contents. G53SRP: Java Threads. Definition. Why we need it. A Simple Embedded System. Why we need it. Java Threads 24/09/2009 G53SRP 1 ADC

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Performance Throughput Utilization of system resources

Java Threads. COMP 585 Noteset #2 1

Synchronized Methods of Old Versions of Java

Concurrency in Java Prof. Stephen A. Edwards

CMSC 433 Programming Language Technologies and Paradigms. Synchronization

What is a thread anyway?

User Space Multithreading. Computer Science, University of Warwick

Multithreaded Programming

Software Practice 1 - Multithreading

THREADS AND CONCURRENCY

Virtual Machine Design

Overview of Java Threads (Part 2)

By: Abhishek Khare (SVIM - INDORE M.P)

Deadlock. Only one process can use the resource at a time but once it s done it can give it back for use by another process.

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

CISC 4700 L01 Network & Client-Server Programming Spring Cowell Chapter 15: Writing Threaded Applications

Threads and Locks. CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015

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

Network Programming COSC 1176/1179. Lecture 6 Concurrent Programming (2)

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

THREADS & CONCURRENCY

CS 556 Distributed Systems

Synchronization in Java

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

Introduction to Locks. Intrinsic Locks

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

SUMMARY INTRODUCTION CONCURRENT PROGRAMMING THREAD S BASICS. Introduction Thread basics. Thread states. Sequence diagrams

Concurrency, Thread. Dongkun Shin, SKKU

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

CSE 374 Programming Concepts & Tools

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

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

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

CPS221 Lecture: Threads

Threads and Too Much Milk! CS439: Principles of Computer Systems January 31, 2018

Threads and Too Much Milk! CS439: Principles of Computer Systems February 6, 2019

Reintroduction to Concurrency

Module - 4 Multi-Threaded Programming

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Threads SPL/2010 SPL/20 1

Chapter 32 Multithreading and Parallel Programming

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

High Performance Computing Course Notes Shared Memory Parallel Programming

CS11 Java. Fall Lecture 7

Multithreaded Programming

Threads & Timers. CSE260, Computer Science B: Honors Stony Brook University

Concurrency & Synchronization. COMPSCI210 Recitation 25th Feb 2013 Vamsi Thummala Slides adapted from Landon Cox

Concurrent Programming

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

CS 159: Parallel Processing


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

CS533 Concepts of Operating Systems. Jonathan Walpole

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

Synchronization SPL/2010 SPL/20 1

Threads, Concurrency, and Parallelism

Synchronization in Concurrent Programming. Amit Gupta

CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion. Tyler Robison Summer 2010

Quiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java

Programming Language Concepts: Lecture 11

Java Threads. Introduction to Java Threads

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

Concurrent Computing CSCI 201 Principles of Software Development

Transcription:

CMSC 433 Programming Language Technologies and Paradigms Spring 2007 Threads and Synchronization May 8, 2007 Computation Abstractions t1 t1 t4 t2 t1 t2 t5 t3 p1 p2 p3 p4 CPU 1 CPU 2 A computer Processes (e.g., JVM s) Threads 2 Processes vs. Threads So, What Is a Thread? int x; foo() { x int x; foo() { x Processes do not share data foo() { x int x; foo() { x Threads share data within a process Conceptually: it is a parallel computation occurring within a process Implementation view: it s a program counter and a stack. The heap and static area are shared among all threads All programs have at least one thread (main) fork() in C 3 4 1

Why Multiple Threads? Performance: Parallelism on multiprocessors Concurrency of computation and I/O Can easily express some programming paradigms Event processing Simulations Keep computations separate, as in an OS Why Not Multiple Threads? Complexity Overhead Higher resource usage 5 6 Programming Threads Threads are available in many languages C, C++, Objective Caml, Java, SmallTalk In many languages (e.g., C and C++), threads are a platform specific add-on Not part of the language specification Part of the Java language specification Java Threads Every application has at least one thread The main thread, started by the JVM to run the application s main() method. The code executed by main() can create other threads Explicitly, using the Thread class Implicitly, by calling libraries that create threads as a consequence RMI, AWT/Swing, Applets, etc. 7 8 2

Java Threads: Creation Java Threads: Creation To explicitly create a thread Instantiate a Thread object An object of class Thread or a subclass of Thread Invoke the object s start() method This will start executing the Thread s run() method concurrently with the current thread Thread terminates when its run() method returns main app new start thread run 9 10 Alternative: The Runnable Interface Extending Thread prohibits a different parent Instead implement Runnable Declares that the class has a void run() method Construct a Thread from the Runnable Constructor Thread(Runnable target) Constructor Thread(Runnable target, String name) Notes: Passing Parameters run() doesn t take parameters We pass parameters to the new thread by storing them as private fields In the extended class Or the Runnable object 11 12 3

Thread Scheduling Once a new thread is created, how does it interact with existing threads? This is a question of scheduling: Given N processors and M threads, which thread(s) should be run at any given time? Thread Scheduling OS schedules a single-threaded process on a single processor Multithreaded process scheduling: One thread per processor Effectively splits a process across CPU s Exploits hardware-level concurrency Many threads per processor Need to share CPU in slices of time 13 14 Scheduling Example (1) Scheduling Example (2) CPU 1 p1 CPU 1 p1 p2 p2 One process per CPU Threads shared between CPU s CPU 2 p1 CPU 2 p1 p2 p2 p2 threads: p1 threads: p2 threads: p1 threads: 15 16 4

Scheduling Consequences Concurrency Different threads from the same application can be running at the same time on different processors Interleaving Threads can be pre-empted at any time in order to schedule other threads 17 Thread Scheduling When multiple threads share a CPU, must decide: When the current thread should stop running What thread to run next A thread can voluntarily yield() the CPU Call to yield may be ignored; don t depend on it Preemptive schedulers can de-schedule the current thread at any time Not all JVMs use preemptive scheduling, so a thread stuck in a loop may never yield by itself. Therefore, put yield() into loops Threads are de-scheduled whenever they block (e.g., on a lock or on I/O) or go to sleep 18 Thread Lifecycle While a thread executes, it goes through a number of different phases New: created but not yet started Runnable: is running, or can run on a free CPU Blocked: waiting for I/O or on a lock Sleeping: paused for a user-specified interval Terminated: completed Which Thread to Run Next? The scheduler looks at all of the runnable threads, including threads that were unblocked because A lock was released I/O became available They finished sleeping, etc. Of these threads, it considers the thread s priority. This can be set with setpriority(). Higher priority threads get preference. Oftentimes, threads waiting for I/O are also preferred. 19 20 5

Simple Thread Methods void start() boolean isalive() void setpriority(int newpriority) Thread scheduler might respect priority void join() throws InterruptedException Waits for a thread to die/finish Simple Static Thread Methods void yield() Give up the CPU void sleep(long milliseconds) throws InterruptedException Sleep for the given period Thread currentthread() Thread object for currently executing thread All apply to thread invoking the method 21 22 Violating Safety Data can be shared by threads Scheduler can interleave or overlap threads arbitrarily Can lead to interference Storage corruption (e.g., a data race/race condition) Violation of representation invariant Violation of a protocol (e.g., A occurs before B) public class Example extends Thread { private // shared state public void run() { public static void main(string args[]) { Thread t1 = new Example(); Thread t2 = new Example(); t1.start(); t2.start(); 23 24 6

Start: both threads ready to run. Each will increment the global count. y = 0 T1 executes, grabbing the global counter value into y. 25 26 y = 0 T1 executes again, storing the counter value y = 0 y = 1 T1 finishes. T2 executes, grabbing the global counter value into y. 27 28 7

But When I Run it Again? y = 0 y = 1 cnt = 2 T2 executes, storing the incremented cnt value. 29 30 Start: both threads ready to run. Each will increment the global count. y = 0 T1 executes, grabbing the global counter value into y. 31 32 8

y = 0 y = 0 T1 is pre-empted. T2 executes, grabbing the global counter value into y. y = 0 y = 0 T2 executes, storing the incremented cnt value. 33 34 What Happened? y = 0 y = 0 T2 completes. T1 executes again, storing the old counter value (1) rather than the new one (2)! In the second example, t1 was preempted after it read the counter but before it stored the new value. Depends on the idea of an atomic action Violated an object invariant A particular way in which the execution of two threads is interleaved is called a schedule. We want to prevent this undesirable schedule. Undesirable schedules can be hard to reproduce, and so hard to debug. 35 36 9

If instead of cnt = y+1; We had written cnt++; Question Would the result be any different? Answer: NO! Don t depend on your intuition about atomicity 37 Question If you run a program with a race condition, will you always get an unexpected result? No! It depends on the scheduler...i.e., which JVM you re running...and on the other threads/processes/etc that are running on the same CPU Race conditions are hard to find 38 Avoiding Interference: Synchronization public class Example extends Thread { private static Object lock = new Object(); public void run() { synchronized (lock) { Lock, for protecting the shared state Acquires the lock; Only succeeds if not held by another thread Releases the lock 39 Applying Synchronization int ; T1 acquires the lock 40 10

Applying Synchronization Applying Synchronization int ; y = 0 T1 reads cnt into y int ; y = 0 T1 is pre-empted. T2 attempts to acquire the lock but fails because it s held by T1, so it blocks 41 42 Applying Synchronization Applying Synchronization int ; y = 0 T1 runs, assigning to cnt int ; y = 0 T1 releases the lock and terminates 43 44 11

Applying Synchronization Applying Synchronization int ; y = 0 T2 now can acquire the lock. int ; y = 0 y = 1 T2 reads cnt into y. 45 46 Applying Synchronization Locks int ; y = 0 y = 1 cnt = 2 T2 assigns cnt, then releases the lock Any Object subclass has (can act as) a lock Only one thread can hold the lock on an object Other threads block until they can acquire it If a thread already holds the lock on an object The thread can reacquire the same lock many times...locks are reentrant Lock is released when object unlocked the corresponding number of times No way to only attempt to acquire a lock Either succeeds, or blocks the thread 47 48 12

Synchronized Statement synchronized (obj) { statements Obtains the lock on obj before executing statements in block Releases the lock when the statement block completes Either normally, or due to a return, break, or exception being thrown in the block Synchronization not a Panacea Two threads can block on locks held by the other; this is called deadlock Object A = new Object(); Object B = new Object(); T1.run() { synchronized (A) { synchronized (B) { T2.run() { synchronized (B) { synchronized (A) { 49 50 Deadlock Quite possible to create code that deadlocks Thread 1 holds lock on A Thread 2 holds lock on B Thread 1 is trying to acquire a lock on B Thread 2 is trying to acquire a lock on A Deadlock! Not easy to detect when deadlock has occurred Other than by the fact that nothing is happening 51 Deadlock: Wait graphs A T1 Thread T1 holds lock A T2 B Thread T2 attempting to acquire lock B Deadlock occurs when there is a cycle in the graph 52 13

Wait graph example Guidelines for Programming with Threads A T2 T1 T1 holds lock on A T2 holds lock on B T1 is trying to acquire a lock on B T2 is trying to acquire a lock on A B 53 Synchronize access to shared data Don t hold multiple locks at a time Could cause deadlock Hold a lock for as little time as possible Reduces blocking waiting for locks While holding a lock, don t call a method you don t understand E.g., a method provided by someone else, especially if you can t be sure what it locks Corollary: document which locks a method acquires 54 14