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

Similar documents
SSC - Concurrency and Multi-threading Advanced topics about liveness

Part I: Communication and Networking

Introduction to Locks. Intrinsic Locks

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

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

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

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

Deadlocks. Deadlock in Resource Sharing Environment. CIT 595 Spring Recap Example. Representing Deadlock

What is a thread anyway?

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.

The Deadlock Problem (1)

! What is a deadlock? ! What causes a deadlock? ! How do you deal with (potential) deadlocks? Maria Hybinette, UGA

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Deadlock. Operating Systems. Autumn CS4023

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

CMSC 433 Programming Language Technologies and Paradigms. Synchronization

Sharing Objects Ch. 3

G52CON: Concepts of Concurrency

Midterm Exam Amy Murphy 19 March 2003

THE UNIVERSITY OF WESTERN AUSTRALIA SAMPLE EXAM QUESTIONS 2007 WITH SOLUTIONS SCHOOL OF COMPUTER SCIENCE CITS3213 CONCURRENT PROGRAMMING (PART II)

Synchronization in Concurrent Programming. Amit Gupta

Chapter 8: Deadlocks. The Deadlock Problem

The Deadlock Problem. Chapter 8: Deadlocks. Bridge Crossing Example. System Model. Deadlock Characterization. Resource-Allocation Graph

Process Synchronisation (contd.) Deadlock. Operating Systems. Spring CS5212

TDDB68 + TDDD82. Lecture: Deadlocks

Concurrency: a crash course

The Deadlock Problem. A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.

System Software Assignment 8 Deadlocks

CS 159: Parallel Processing

G52CON: Concepts of Concurrency

F. Tip and M. Weintraub CONCURRENCY

OPERATING SYSTEMS. Prescribed Text Book. Operating System Principles, Seventh Edition. Abraham Silberschatz, Peter Baer Galvin and Greg Gagne

Threads Questions Important Questions

!! What is a deadlock? !! What causes a deadlock? !! How do you deal with (potential) deadlocks? Maria Hybinette, UGA

Last Class: Monitors. Real-world Examples

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

Threads Synchronization

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

Deadlock Prevention. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Discussion CSE 224. Week 4

System Model. Types of resources Reusable Resources Consumable Resources

Question Points Score Total 100

EECS 482 Introduction to Operating Systems

Concurrent Programming Synchronisation. CISTER Summer Internship 2017

Chapter 8: Deadlocks. Bridge Crossing Example. The Deadlock Problem

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

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

G52CON: Concepts of Concurrency

Deadlocks. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

CS 10: Problem solving via Object Oriented Programming Winter 2017

Principles of Software Construction: Objects, Design and Concurrency. The Perils of Concurrency, Part 2 Can't live with it. Cant live without it.

Principles of Software Construction: Objects, Design and Concurrency. The Perils of Concurrency, Part 2 (Can't live with it, can't live without it.

OpenMP and more Deadlock 2/16/18

Readings and References. Deadlock. Deadlock. Simple Traffic Gridlock Example. Reading. CSE Computer Systems November 30, 2001.

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

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

CS 318 Principles of Operating Systems

Fall 2015 COMP Operating Systems. Lab 06

Lecture 8: September 30

CS370 Operating Systems

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Deadlocks. Dr. Yingwu Zhu

POSIX Threads: a first step toward parallel programming. George Bosilca

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Introduction to OS. Deadlock. MOS Ch. 6. Mahmoud El-Gayyar. Mahmoud El-Gayyar / Introduction to OS 1

Threads and Parallelism in Java

Operating Systems. Synchronization

CS370 Operating Systems

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

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

CS 351 Design of Large Programs Threads and Concurrency

CS153: Deadlock. Chengyu Song. Slides modified from Harsha Madhyvasta, Nael Abu-Ghazaleh, and Zhiyun Qian

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Deadlock. Readings and References. Simple Traffic Gridlock Example. Deadlock. Reading. Other References. CSE 410, Spring 2006 Computer Systems

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

CMSC 132: Object-Oriented Programming II

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

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

Deadlock Prevention. Deadlock can be prevented if 1 of the 4 conditions cannot hold

Deadlocks: Part I Prevention and Avoidance Yi Shi Fall 2017 Xi an Jiaotong University

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Synchronization in Java

Chapter 7: Deadlocks. Operating System Concepts with Java 8 th Edition

Java Memory Model. Jian Cao. Department of Electrical and Computer Engineering Rice University. Sep 22, 2016

MODERN OPERATING SYSTEMS. Third Edition ANDREW S. TANENBAUM. Chapter 6 Deadlocks

Concurrency and Synchronisation

Concurrency and Synchronisation

UNIT:2. Process Management

Quiz Answers. CS 537 Lecture 9 Deadlock. What can go wrong? Readers and Writers Monitor Example

Java Threads and intrinsic locks

The Java Memory Model

Chapter 7: Deadlocks 1

Operating Systems 2015 Spring by Euiseong Seo DEAD LOCK

Concurrency: Deadlock and Starvation. Chapter 6

Java Monitor Objects: Synchronization (Part 1)

Resource management. Real-Time Systems. Resource management. Resource management

Transcription:

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (II) Shan He School for Computational Science University of Birmingham Module 06-19321: SSC

Outline Outline of Topics Review what we have learned More about Synchronized methods Liveness: deadlock

Review what we have learned High level synchronisation in Java High level concurrency tools Executors framework Fork/Join framework

Review what we have learned Low level synchronisation in Java Low level concurrency tools Volatile Variables Locks Atomic Operations Reentrant Locks Synchronized keyword Synchronized methods Synchronized statements

More about Synchronized methods How to use Java synchronized methods Simply add the synchronized keyword to method declaration: Example (See my Java source code for details): for a method public void add(long value){...} the synchronised method is: public synchronized void add(long value){...} The synchronized keyword automatically acquires the intrinsic lock for that method s object and releases it when the method finishes. A synchronized method: prevents invocations of synchronized methods on the same object; and automatically establishes a happens-before relationship

More about Synchronized methods Java synchronized methods: how do they work? Steps: Step 1: Thread A executes a synchronized method Step 2: Thread A automatically acquires the intrinsic lock for that method s object Step 3: Suspend execution of other threads that invoke synchronized methods for the same object since they don t have the intrinsic lock Step 4: Thread A releases the intrinsic lock when it is finished with the object. Step 5: Another thread scheduled by the scheduling algorithm acquires the intrinsic lock to subsequently execute its synchronised methods for the same object Note: Java guarantees that changes to the state of the object by a thread are visible to all threads.

More about Synchronized methods Java synchronized methods: drawbacks A synchronised method synchronises on the method s object instance or the class: cannot synchronise some parts of your code coarse grained, lack of granular control over lock, Thread synchronises on the whole object instead of a smaller critical section slow; You cannot set an intrinsic lock on an external object using synchronized methods.

Also called synchronized blocks, which can solve all the drawbacks of synchronized methods. Use to mark a block of code as synchronized The block is also called critical section where one and only one thread is executing Usage: Use the following statement to mark a synchronized block: synchronized(object){ synchronize operations on the object }

How to use Example 1 (See my Java source code for details): public void add(int value) { synchronized(this){ this.count += value;} } The above code is equivalent to: public synchronized void add(int value) { this.count += value;}

Synchronized statements must specify the object that provides the intrinsic lock. e.g., synchronized(object) In Example 1, we use synchronized(this){...} The object this is the instance the add method is called on The object specify by the synchronized statement is called a monitor object The code is said to be synchronized on the monitor object Only one thread can execute inside a Java code block synchronized on the same monitor object.

Question: synchronize operations on object Is the following code correct? long r1 = 1; synchronized(r1) { r1++; }

Answer: synchronize operations on object We can only synchronize on objects but long is a primitive value that doesn t have a lock We we can use Long which is an object: Long r1 = 1; synchronized(r1) { r1++; }

Question: synchronised on a static method If you have a static method, whether synchronized methods still work? Reminder 1: a static method belongs to the class, not to the specific objects of that class, that is, a static method has no associated object. Reminder 2: Synchronized methods prevent invocations of synchronized methods on the same object

Answer: synchronising a class For a class (static) method, the monitor associated with the Class object for the method s class is used. For example, synchronized static void foo() {... } is equivalent to static void foo() { synchronized(someclass.class) {... } } If one thread is executing a static synchronized method, any other threads executing any static synchronized methods in the same class will be blocked. However, non-static synchronized methods will not be blocked since they require the lock on the instance, i.e., synchronized(this){...}

Liveness: deadlock Liveness and Deadlock Liveness property in concurrent programming means something good eventually happens. More specifically, the ability of an application to execute in a timely manner. Liveness problems include deadlock, starvation and livelock Deadlock: a situation threads are blocked forever, waiting for each other: Can be two threads waiting for another to release a lock Can be more than two threads are waiting for resources in a circular chain

Liveness: deadlock 4 Necessary conditions for deadlock A deadlock situation will happen if all of the following conditions (called Coffman conditions) hold simultaneously: Mutual exclusion: at least one resource must be held in a non-sharable mode: Only one thread at a time can use the resource If another thread requests that resource, the requesting thread must wait until the resource has been released Hold and wait: Thread holds one resource while waits for another No preemption: Once a thread is holding a resource, then that resource cannot be taken away from that thread until the thread voluntarily releases it. Circular wait: A thread must be waiting for a resource which is being held by another thread, which in turn is waiting for the first thread to release the resource.

Liveness: deadlock Deadlock A Java example