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

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

More thread safety and Concurrent collections. Nasser Giacaman CompSci 230: Software Design and Construction

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

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

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

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

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

Threads and Java Memory Model

CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages

Models of concurrency & synchronization algorithms

Threads Questions Important Questions

G52CON: Concepts of Concurrency

Synchronization COMPSCI 386

Lecture 16: Thread Safety in Java

CS 351 Design of Large Programs Threads and Concurrency

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

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

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

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

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

CONCURRENT API AND PARALLEL PROGRAMMING

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

CS 159: Parallel Processing

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

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

Part I: Communication and Networking

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

Lecture 03: Thread API (continue)

Problems with Concurrency. February 19, 2014

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

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

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

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

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

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

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

Introduction to OS Synchronization MOS 2.3

Java Threads. COMP 585 Noteset #2 1

Concurrency: a crash course

CS11 Java. Fall Lecture 7

Atomic Variables & Nonblocking Synchronization

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

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

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

Processes and Threads. Industrial Programming. Processes and Threads (cont'd) Processes and Threads (cont'd)

SSC - Concurrency and Multi-threading Advanced topics about liveness

Pre- and post- CS protocols. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 7. Other requirements for a mutual exclusion algorithm

The University of Texas at Arlington

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

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

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

G52CON: Concepts of Concurrency

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Chapter 32 Multithreading and Parallel Programming

JFokus Finding And Solving Java Deadlocks

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

Systems Programming & Scripting

Java Programming Lecture 23

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

Synchronization SPL/2010 SPL/20 1

The University of Texas at Arlington

What is a thread anyway?

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

IT 540 Operating Systems ECE519 Advanced Operating Systems

Java ReentrantLock (Part 1)

Lecture 8: September 30

THREADS AND CONCURRENCY

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

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

Process Coordination and Shared Data

CMSC 132: Object-Oriented Programming II

Handouts. 1 Handout for today! Recap. Homework #2 feedback. Last Time. What did you think? HW3a: ThreadBank. Today. Small assignment.

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

Dr.-Ing. Michael Eichberg

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

Introduction to Locks. Intrinsic Locks

Lecture 29: Java s synchronized statement

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

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

Synchronising Threads

Concurrency in Java Prof. Stephen A. Edwards

CST242 Concurrency Page 1

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

Problems with Concurrency

Concurrency in Object Oriented Programs 1. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Java theory and practice: Going atomic The new atomic classes are the hidden gems of java.util.concurrent

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects

Dealing with Issues for Interprocess Communication

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

Dr.-Ing. Michael Eichberg

Synchronized Methods of Old Versions of Java

Synchronization. Announcements. Concurrent Programs. Race Conditions. Race Conditions 11/9/17. Purpose of this lecture. A8 released today, Due: 11/21

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

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

MultiJav: A Distributed Shared Memory System Based on Multiple Java Virtual Machines. MultiJav: Introduction

Multithreading. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

Multitasking / Multithreading system Supports multiple tasks

Transcription:

Lecture 07: Advanced Thread programming Software System Components 2 Behzad Bordbar School of Computer Science, University of Birmingham, UK Recap How to deal with race condition in Java Using synchronised Mutual exclusion over a class Mutual exclusion over a method Mutual exclusion over a snippet of code Locking using Lock interface Lecture 07 1 Lecture 07 2 Contents Reenterancy Explicit Lock vs. Intrinsic lock Limitations of intrinsic lock volatile Atomic variables Thread safety Condition interface Reenterancy of synchronized ( is also called intrinsic lock) When a thread requests a lock that is already held by another thread, it gets blocked intrinsic locks are reentrant, if a thread tries to acquire a lock that it already holds, the request succeeds. Reentrancy means that locks are acquired on a per-thread rather than per-invocation basis. Lecture 07 3 Lecture 07 4 Reenterancy of synchronise (ctd) Reentrancy associate with each lock: acquisition count owning thread When a thread acquires a previously unheld lock, the JVM records the owner and sets the acquisition count to one. If that same thread acquires the lock again, the count is incremented, and when the owning thread exits the synchronized block, the count is decremented. When the count reaches zero, the lock is released. Lecture 07 5 Explicit Locks: ReentrantLock ReentrantLock implements Lock, new to Java5 ReentrantLock provides all capabilities provided by intrinsic lock (synchronised): mutual exclusion and memory-visibility guarantees as synchronized. Acquiring a ReentrantLock behaves the same as entering a synchronized block, Releasing a ReentrantLock behaves the same as exiting a synchronized block. But why a new mechanism? Intrinsic lock has some limitations Lecture 07 6 1

Limitations of intrinsic lock It is not possible to interrupt a thread waiting to acquire a lock, (Interruptible Lock Acquisition in explicit lock) It is not possible to attempt to acquire a lock without being willing to wait for it forever (Polled Timed Lock Acquisition in explicit lock Java 5.0). Intrinsic locks also must be released in the same block of code in which they are acquired; this simplifies coding and interacts with exception handling, but makes non-block structured locking impossible (Nonblock-structured Locking in explicit lock) However, in explicit lock failing to use finally to release the lock makes the debugging very hard Volatile It is not possible to designate an attribute as synchronized Suppose a class with an attribute value which is accessed via a synchronized method of the class. But what if other objects access it directly via another thread. Old lesson: make attributes private and use synchronised mutator and accessors (if accessed by multiple threads) If an attribute is used directly by multiple threads and the access is not channelled through synchronised mutator and accessorit must be made volatile Lecture 07 7 Lecture 07 8 See the API for new features. Volatile (ctd) For the sake of efficiency, the compiler loads the value of a variable which is used several times into one of run-time registers Use of volatile signals the compiler to reload the attribute s value directly from its memory location on each use This only applies to class variables and instances, variables and methos within a thread have their own copy on the run-time stack and are not going to be interfered with Lecture 07 9 Atomic variable Java 5 introduces atomic variables to support atomic access to single variables in java.util.concurrent.atomic AtomicBoolean AtomicInteger AtomicIntegerArray AtomicLong AtomicReference<V> // V type of object AtomicReferenceArray<E> For example AtomicInteger: Lecture 07 10 Atomic variable class Counter { private int c = 0; public void increment() { c++; class Counter { private int c = 0; public synchronized void increment() { c++; class AtomicCounter { private AtomicInteger c = new AtomicInteger(0); public void increment() { c.incrementandget(); Lecture 07 11 Thread safety a class takes proper care to protect its fields with locks, so that it can work with multiple threads, then it is called thread safe. Swing is not thread safe the general rule is to manipulate Swing components only on the event dispatch thread. Consider repaint-paint pattern: repaint can be called on any thread, but it doesn't directly do the repainting. Instead a paint is put on the queue of jobs for the event dispatch thread to execute. Exceptions: settext can normally be called from any thread a component has not yet been set visible or added to a visible container, then any thread (typically the main thread when it creates objects) can safely Lecture work 07 on it. 12 2

Liveness A concurrent application's ability to execute in a timely manner is known as its liveness. deadlock starvation livelock Lecture 07 13 Deadlock Locks are intended to ensure safety, blocking threads that might interfere with each other and forcing them to wait. Sometimes this can go too far: Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. Example of deadlock: Suppose there are two shared resources, protected by locks la and lb. A thread that wishes to use the resources must acquire both resources. Now suppose one Lecture 07 14 method acquires la first: Deadlock (ctd) la.lock(); lb.lock(); try { critical region finally { la.unlock(); lb.unlock(); while another method acquires them in the reverse order lb first, then la. If two threads call the two methods, then they might reach deadlock with each thread owning one of the locks but unable to acquire the other. Lecture 07 15 Starvation Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. happens when shared resources are made unavailable for long periods by "greedy" threads. Lecture 07 16 Livelock A thread often acts in response to the action of another thread. If the other thread's action is also a response to the action of another thread, then livelock may result. As with deadlock, livelocked threads are unable to make further progress. However, the threads are not blocked they are simply too busy responding to each other to resume work Waiting for a state of affairs One form of deadlock is the following. A thread holds a lock l, but it cannot proceed because it is waiting for a certain state of affairs. Unfortunately, no other thread can bring about that state of affairs, because they would first need to acquire the lock l. A condition is a way in which the first thread can temporarily release the lock so that other threads can acquire it. Lecture 07 17 Lecture 07 18 3

java.util.concurrent.locks. Condition interface Each condition is attached to a particular lock, and is created by calling the factory method newcondition() on that lock. There are two Condition methods that work together: await() signalall(). When a thread calls await() it releases the lock and is blocked. Lecture 07 19 java.util.concurrent.locks. Condition interface (ctd) await() also throws InterruptedException in the same way as Thread.sleep(), so again you have to decide how to handle those exceptions. A thread should only call await() or signalall() on a condition if it already owns the corresponding lock. Otherwise an exception will normally be thrown. Lecture 07 20 java.util.concurrent.locks. Condition interface (ctd) The condition object keeps track of all the threads that are waiting on it. Normally they stay blocked until another thread calls signalall() on the condition. At that point all the waiting threads are allowed to run again, though they still have to wait their turn to acquire the lock. When a thread owns the lock again, it is allowed at last to return from await() and continue its execution. Lecture 07 21 Using a condition await() is called by a thread when it owns the lock but also needs that testable property to hold and discovers that it does not. By calling await(), the thread says, "I can't go on, I might as well be blocked, and I'll release the lock. Almost always, the await() is in a loop like this. while (property is false) { condition.await(); When the thread finishes the loop, it knows the property is true, and it also owns the lock so no other thread can access the resource and make the property go false unexpectedly. Lecture 07 22 Let us rework the TravelAgent example, create 3 classes: 1. Flight.java with attributes: TotalNumberOfSeats and BookedTillNow. Include a constructor to use the fields. Add two methods: ReserveSeat() // for booking a pair of seats CancelReservation() // for cancelling a pair of seats. Reservation takes 1 secs and Cancelation takes 5 secs 2. Threades classes ReservingTravelAgent.java, with Attributes: int ID; // Identity number of the flight Flight flight; Lecture 07 23 In the run() method invoke ReserveSeat() of flight, to reserve a pair of seats 3. CancellingTravelAgent.java with attributes int ID; // Identity number of the flight Flight flight; In the run() method invoke CancelReservation() of flight ((for a sample solution see reworkedtravelagent01 directory)) Lecture 07 24 4

In the run() method invoke ReserveSeat() of flight, to reserve a pair of seats 3. CancellingTravelAgent.java with attributes int ID; // Identity number of the flight Flight flight; In the run() method invoke CancelReservation() of flight ((for a sample solution see reworkedtravelagent01 directory)) Lecture 07 25 In Flight class BookedTillNow is a shared resource. Use an explicit lock to make sure correct access via multiple threads to the two methods. Do this by adding a new lock ((for a solution see reworkedtravelagent02 directory)) Use Condition interface to modify the code to allow cancellation of seats and reservation of a pair of seats which are cancelled. ((for a solution see reworkedtravelagent03 directory)) Lecture 07 26 5