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

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

CS455: Introduction to Distributed Systems [Spring 2019] Dept. Of Computer Science, Colorado State University

CMSC 330: Organization of Programming Languages

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

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

Java ReentrantLock (Part 1)

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

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

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

Java Threads. Introduction to Java Threads

Concurrent Programming using Threads

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

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

Models of concurrency & synchronization algorithms

Threads Questions Important Questions

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

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

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

CMSC 330: Organization of Programming Languages

G52CON: Concepts of Concurrency

Java Threads. COMP 585 Noteset #2 1

What is a thread anyway?

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

Concurrency in Java Prof. Stephen A. Edwards

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

Chapter 32 Multithreading and Parallel Programming

An Introduction to Programming with Java Threads Andrew Whitaker University of Washington 9/13/2006. Thread Creation

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

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

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

Concurrent Queues, Monitors, and the ABA problem. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University

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

Dr.-Ing. Michael Eichberg

Only one thread can own a specific monitor

Introduction to OS Synchronization MOS 2.3

What's wrong with Semaphores?

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

Synchronization in Java

CS11 Java. Fall Lecture 7

CSE332: Data Abstractions Lecture 19: Mutual Exclusion and Locking

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

CS 351 Design of Large Programs Threads and Concurrency

Synchronization I. Jo, Heeseung

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

Monitors; Software Transactional Memory

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Monitors; Software Transactional Memory

Introduction to Java Threads

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

Faculty of Computers & Information Computer Science Department

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

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

Threads and Java Memory Model

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

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

Advanced Concepts of Programming

JFokus Finding And Solving Java Deadlocks

Concurrency, Thread. Dongkun Shin, SKKU

Producing Production Quality Software. Lecture 12: Concurrent and Distributed Programming Prof. Arthur P. Goldberg Fall, 2004

Frequently asked questions from the previous class survey

Programming Language Concepts: Lecture 11

Lecture 8: September 30

Lecture 29: Java s synchronized statement

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

Problems with Concurrency. February 19, 2014

Threads Synchronization

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

Synchronized Methods of Old Versions of Java

Concurrent Programming

CSE 153 Design of Operating Systems

Frequently asked questions from the previous class survey

Synchronization in Concurrent Programming. Amit Gupta

7. MULTITHREDED PROGRAMMING

User Space Multithreading. Computer Science, University of Warwick

Get out, you will, of this bind If, your objects, you have confined

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

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

Java Threads Vs Processes. CS Concurrent Programming. Java Threads. What can a thread do? Java Concurrency

Synchronization Lecture 24 Fall 2018

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

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

Dr.-Ing. Michael Eichberg

Program Graph. Lecture 25: Parallelism & Concurrency. Performance. What does it mean?

10/17/2011. Cooperating Processes. Synchronization 1. Example: Producer Consumer (3) Example

Concurrent Programming Lecture 10

CS 159: Parallel Processing

Threads and Locks, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS455: Introduction to Distributed Systems [Spring 2018] Dept. Of Computer Science, Colorado State University

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

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

OS06: Monitors in Java

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

Multi-threading in Java. Jeff HUANG

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

Transcription:

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Threads block when they can t get that lock Wanna have your threads stall? Go ahead, synchronize it all The antidote to this liveness pitfall? Keeping the lock scope small Shrideep Pallickara Computer Science Colorado State University L7.1 Frequently asked questions from the previous class survey User thread stacks use virtual memory, so why worry? Should we never use join()? Can it cause a deadlock? Does a thread release locks when it terminates? What s a good reason to interrupt a thread? Is it common to terminate threads by throwing unchecked exceptions? What if we have correct implementations of suspend() and resume()? Can we then use it? No! L7.2 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.1

Topics covered in this lecture Data synchronization Synchronized blocks Lock fairness Wait-notify L7.3 Heisenbugs Term coined by ACM Turing Award winner Jim Gray Pun on the name of Werner Heisenberg Act of observing a system, alters its state! Describes a particular class of bugs Those that disappear or change behavior when you try to examine them Multithreaded programs are a common source of Heisenbugs L7.4 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.2

What about regular bugs? Sometimes referred to as Bohr bugs Deterministic Generally much easier to diagnose L7.5 Two friends plan to meet at Starbucks But there are two Starbucks on College Avenue @ the First Starbucks Store 12:10 A is looking for friend B 12:15 A leaves for the second store 12:20 B arrives at store 12:30 12:40 B is Looking for friend A B leaves for the second store @ the Second Starbucks Store B is Looking for friend A B leaves for the first store A arrives at store A is looking for friend B A leaves for the first store Both friends are now frustrated and undercaffeinated! L7.6 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.3

DATA SYNCHRONIZATION L7.7 Why sharing data between threads is problematic Race conditions Threads attempt to access data more or less simultaneously A thread may change the value of data that some other thread is operating on L7.8 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.4

Example code with race condition public class MyThread extends Thread { private byte[] values; private int position; public void modifydata(byte[] newvalues, int newposition) {... Modify values and position public void utilizedataandperformfunction() {... Use values and position public void run() {... Main logic L7.9 In the previous snippet a race condition exists because The thread that calls modifydata() is accessing the same data as the thread that calls utilizedataandperformfunction() utilizedataandperformfunction() and modifydata() are not atomic It is possible that values and position are changed while they are being used L7.10 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.5

What is atomic? The code cannot be interrupted during its execution Accomplished in hardware or simulated in software Code that cannot be found in an intermediate state L7.11 Eliminating the race condition using the synchronized keyword If we declared both modifydata() and utilizedataandperformfunction() as synchronized? Only one thread gets to call either method at a time n Only one thread accesses data at a time When one thread calls one of these methods, while another is executing one of them? n The second thread must wait L7.12 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.6

Example code with no race conditions by using the synchronized keyword public class MyThread extends Thread { private byte[] values; private int position; public void synchronized modifydata(byte[] newvalues, int newposition) {... Modify values and position public void synchronized utilizedataandperformfunction() {... Use values and position public void run() {... Main logic L7.13 Revisiting the mutex lock Mutually exclusive lock If two threads try to grab a mutex? Only one succeeds In Java every object has an associated lock L7.14 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.7

When a method is declared synchronized The thread that wants to execute the method must acquire a lock Once the thread has acquired the lock? It executes method and releases the lock When a method returns, the lock is released Even if the return is because of an exception L7.15 Locks and objects There is only one lock per object If two threads call synchronized methods of the same object? Only one can execute immediately n The other has to wait until the lock is released L7.16 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.8

Another code snippet to look at public class MyThread extends Thread { private boolean done = false; public void run() { while (!done) {... Main logic public void setdone(boolean isdone) { done = isdone; L7.17 Can t we just synchronize the two methods as we did previously? If we synchronized both run() and setdone()? setdone() would never execute! The run() method does not exit until the done flag is set But the done flag cannot be set because setdone() cannot execute till run() completes Uh oh L7.18 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.9

The problem stems from the scope of the lock Scope of a lock Period between grabbing and releasing a lock Scope of the run() method is too large! Lock is grabbed and never released We will look at techniques to shrink the scope of the lock But let s look at another solution for now L7.19 Let s look at operations performed on the data item (done) The setdone() method stores a value into the flag The run() method reads the value In our previous example: Threads were accessing multiple pieces of data No way to update multiple data items atomically without the synchronized keyword L7.20 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.10

But Java specifies that the loading and storing of variables is atomic Except for long and double variables The setdone() should be atomic The run() method has only one read operation of the data item The race condition should not exist But why is it there? L7.21 Threads are allowed to hold values of variables in registers When one thread changes the value of the variable? Another thread may not see the changed variable This is particularly true in loops controlled by a variable Looping thread loads value of variable in register and does not notice when value is changed by another thread L7.22 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.11

Two approaches to solving this Providing setter and getter methods for variable and using the synchronized keyword When lock is acquired, temporary values stored in registers are flushed to main memory The volatile keyword Much cleaner solution L7.23 If a variable is marked as volatile Every time it is used? Must be read from main memory Every time it is written? Must be written to main memory Load and store operations are atomic Even for long and double variables L7.24 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.12

Some more about volatile variables Prior to JDK 1.2 variables were always read from main memory Using volatile variables was moot Subsequent versions introduced memory models and optimizations L7.25 Synchronization and the volatile keyword Can be used only when operations use a single load and store Operations like ++, --? n Load-change-store The volatile keyword forces the JVM to not make temporary copies of a variable Declaring an array volatile? The reference becomes volatile The individual elements are not volatile L7.26 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.13

SYNCHRONIZED METHODS & LOCKS L7.27 Synchronizing methods Not possible to execute the same method in one thread while Method is running in another thread If two different synchronized methods in an object are called? They both require the lock of the same object Two or more synchronized methods of the same object can never run in parallel in separate threads L7.28 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.14

A lock is based on a specific instance of an object Not on a particular method or class Suppose we have 2 objects: objecta and objectb with synchronized methods modifydata() and utilizedata() One thread can execute objecta.modifydata() while another executes objectb.utilizedata() in parallel Two different locks are grabbed by two different threads No need for threads to wait for each other L7.29 How does a synchronized method behave in conjunction with an unsynchronized one? Synchronized methods try to grab the object lock Only 1 synchronized method in a object can run at a time provides data protection Unsynchronized methods Don t grab the object lock Can execute at any time by any thread n Regardless of whether a synchronized method is running L7.30 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.15

For a given object, at any time Any number of unsynchronized methods may be executing But only 1 synchronized method can execute L7.31 Synchronizing static methods A lock can be obtained for each class The class lock The class lock is the object lock of the Class object that models the class There is only 1 Class object per class Allows us to achieve synchronization for static methods L7.32 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.16

Object locks and class locks Are not operationally related The class lock can be grabbed and released independently of the object lock If a non-static synchronized method calls a static synchronized method? It acquires both locks L7.33 EXPLICIT LOCKING L7.34 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.17

The synchronized keyword Serializes accesses to synchronized methods in an object Not suitable for controlling lock scope in certain situations Can be too primitive in some cases L7.35 Many synchronization schemes in J2SE 5.0 onwards implement the Lock interface Two important methods lock() and unlock() Similar to using the synchronized keyword Call lock() at the start of the method Call unlock() at the end of the method Difference: we have an actual object that represents the lock Store, pass around, or discard L7.36 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.18

Semantics of the using Lock If another thread owns the lock Thread that attempts to acquire the lock must wait until the other thread calls unlock() Once the waiting thread acquires the lock, it returns from the lock() method L7.37 Using the Lock interface public class DataOpertor { private Lock datalock = new ReentrantLock(); public void modifydata(byte[] newvalues, int newposition) { try { datalock.lock();... Modify values and position finally { datalock.unlock(); public void utilizedataandperformfunction() { try { datalock.lock();... Use values and position finally { datalock.unlock(); L7.38 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.19

Advantages of using the Lock interface Grab and release locks whenever we want Now possible for two objects to share the same lock Lock is no longer attached to the object whose method is being called Can be attached to data, groups of data, etc. Not objects containing the executing methods L7.39 Advantages of explicit locking We can move them anywhere to adjust lock scope Can span from a line of code to a scope that encompasses multiple methods and objects Lock at scope specific to problem Not just the object L7.40 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.20

SYNCHRONIZED BLOCKS L7.41 Much of what we accomplish with the Lock we can do so with the synchronized keyword public class DataOperator { public void modifydata(byte[] newvalues, int newposition) { synchronized(this) {... Modify values and position public void utilizedataandperformfunction() { synchronized(this) {... Use values and position L7.42 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.21

Synchronized methods vs. Synchronized Blocks Possible to use only the synchronized block mechanism to synchronize whole method You decide when it s best to synchronize a block of code or the whole method RULE: Establish as small a lock scope as possible L7.43 The Lock interface [java.util.concurrent.locks] public interface Lock { public void lock(); public void lockinterruptibly() throws InterruptedException; public boolean trylock(); public boolean trylock(long time, TimeUnit unit) throws InterruptedException; public void unlock(); public Condition newcondition(); L7.44 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.22

Lock Fairness ReentrantLock allows locks to be granted fairly Locks are granted as close to arrival order as possible Prevents lock starvation from happening Possibilities for granting locks 1 2 3 First-come-first-served Allows servicing the maximum number of requests Do what s best for the platform L7.45 THREAD NOTIFICATIONS L7.46 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.23

Objects and communications Every object has a lock Every object also includes mechanisms that allow it to be a waiting area Allows communication between threads L7.47 Conditions One thread needs a condition to exist Assumes another thread will create that condition When another thread creates the condition? It notifies the first thread that has been waiting for that condition L7.48 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.24

wait(), notify() and the Object class public class Object { public void wait(); public void wait(long timeout); public void notify(); L7.49 wait(), notify() and the Object class Wait-and-notify mechanisms are available for every object Accomplished by method invocations Synchronized mechanism is handled by using a keyword L7.50 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.25

Wait-and-notify relate to synchronization, but It is more of a communications mechanism Allows one thread to communicate to another that a condition has occurred Does not specify what that specific condition is L7.51 Can wait-and-notify replace the synchronized mechanism? No Does not solve the race condition that the synchronized mechanism solves Must be used in conjunction with the synchronized lock Prevents race condition that exists in the wait-notify mechanism itself L7.52 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.26

A code snippet that uses wait-notify to control the execution of the thread public class Tester implements Runnable { private boolean done = true; public synchronized run() { while (true) { if (done) wait(); else {... Logic... wait(100); public synchronized void setdone(boolean b) { done = b; if (!done) notify(); L7.53 About the wait() method When wait() executes, the synchronization lock is released By the JVM When a notification is received? The thread needs to reacquire the synchronization lock before returning from wait() L7.54 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.27

Integration of wait-notify and synchronization Tightly integrated with the synchronization lock Feature not directly available to us Not possible to implement this: native method This is typical of approach in other libraries Condition variables for Solaris and POSIX threads require that a mutex lock be held L7.55 Details of the race condition in the wait-notify mechanism The first thread tests the condition and confirms that it must wait The second thread sets the condition The second thread calls notify() This goes unheard because the first thread is not yet waiting The first thread calls wait() L7.56 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.28

How does the potential race condition get resolved? To call wait() or notify() Obtain lock for the object on which this is being invoked It seems as if the lock has been held for the entire wait() invocation, but 1 wait() releases lock prior to waiting 2 Reacquires the lock just before returning from wait() L7.57 Is there a race condition during the time wait() releases and reacquires the lock? wait() is tightly integrated with the lock mechanism Object lock is not freed until the waiting thread is in a state in which it can receive notifications System prevents race conditions from occurring here L7.58 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.29

If a thread receives a notification is it guaranteed that condition is set? No Prior to calling wait(), test condition while holding lock Upon returning from wait() retest condition to see if you should wait() again L7.59 What if notify() is called and no thread is waiting? Wait-and-notify mechanism has no knowledge about the condition about which it notifies If notify() is called when no other thread is waiting? The notification is lost L7.60 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.30

What happens when more than 1 thread is waiting for a notification? Language specification does not define which thread gets the notification Based on JVM implementation, scheduling and timing issues No way to determine which thread will get the notification L7.61 notifyall() All threads that are waiting on an object are notified When threads receive this, they must work out 1 Which thread should continue 2 Which thread(s) should call wait() again n All threads wake up, but they still have to reacquire the object lock n Must wait for the lock to be freed L7.62 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.31

The contents of this slide-set are based on the following references Java Threads. Scott Oaks and Henry Wong. 3rd Edition. O Reilly Press. ISBN: 0-596- 00782-5/978-0-596-00782-9. [Chapter 3, 4] Operating Systems Principles and Practice. Thomas Anderson and Michael Dahlin. 2 nd Edition. ISBN: 978-0985673529. [Chapter 5] L7.63 SLIDES CREATED BY: SHRIDEEP PALLICKARA L7.32