Introduction to Locks. Intrinsic Locks

Similar documents
Synchronization SPL/2010 SPL/20 1

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

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

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

Synchronising Threads

CMSC 433 Spring 2013 Exam 1

Concurrency: a crash course

CS 159: Parallel Processing

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

Shared Mutable State SWEN-220

CS11 Java. Fall Lecture 7

Safety SPL/2010 SPL/20 1

CMSC 330: Organization of Programming Languages

Threads Synchronization

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

Java Monitor Objects: Synchronization (Part 1)

Advanced concurrent programming in Java Shared objects

Midterm #1. CMSC 433: Programming Language Technologies and Paradigms. October 14, 2013

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

Threads and Parallelism in Java

Sharing Objects Ch. 3

CMSC131. Library Classes

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

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

Advanced MEIC. (Lesson #18)

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

Multiple Inheritance. Computer object can be viewed as

System Programming. Practical Session 4: Threads and Concurrency / Safety

CMSC131. How Objects Can (or Can't) Change

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

Today: Synchronization. Recap: Synchronization

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

Agenda. Highlight issues with multi threaded programming Introduce thread synchronization primitives Introduce thread safe collections

Question Points Score Total 100

Java SE 8 Programming

Threads and Java Memory Model

Atomicity CS 2110 Fall 2017

Synchronization in Java

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

Synchronization in Concurrent Programming. Amit Gupta

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

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

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

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

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

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

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

SSC - Concurrency and Multi-threading Advanced topics about liveness

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects

CSE 374 Programming Concepts & Tools

Java SE 8 Programming

Safety and liveness for critical sections

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

Threads Questions Important Questions

Synchronization for Concurrent Tasks

Discussion CSE 224. Week 4

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

Programmazione di sistemi multicore

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Java SE 8 Programming

CS 160: Interactive Programming

CSE332: Data Abstractions Lecture 19: Mutual Exclusion and Locking

Programming Languages

CS510 Advanced Topics in Concurrency. Jonathan Walpole

The University of Texas at Arlington

CMSC 330: Organization of Programming Languages

CMSC 132: Object-Oriented Programming II

CMSC421: Principles of Operating Systems

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

Page 1. Challenges" Concurrency" CS162 Operating Systems and Systems Programming Lecture 4. Synchronization, Atomic operations, Locks"

Last Class: Deadlocks. Today

The University of Texas at Arlington

CSE 451 Midterm 1. Name:

Java Concurrency. Towards a better life By - -

Java Threads and intrinsic locks

The deadlock problem

G52CON: Concepts of Concurrency

Stuart

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

Midterm Exam March 13, 2013 CS162 Operating Systems

Lecture 16: Thread Safety in Java

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

Programmazione di sistemi multicore

Synchronization I. Jo, Heeseung

EECS 482 Introduction to Operating Systems

The Java Memory Model

CS 450 Exam 2 Mon. 11/7/2016

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

CMSC 433 Programming Language Technologies and Paradigms. Sharing Objects

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

CSE 332: Data Structures & Parallelism Lecture 17: Shared-Memory Concurrency & Mutual Exclusion. Ruth Anderson Winter 2019

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Atomicity via Source-to-Source Translation

Programming in Parallel COMP755

CSE 332: Locks and Deadlocks. Richard Anderson, Steve Seitz Winter 2014

CS 351 Design of Large Programs Threads and Concurrency

Lecture #7: Implementing Mutual Exclusion

Executive Summary. It is important for a Java Programmer to understand the power and limitations of concurrent programming in Java using threads.

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

Transcription:

CMSC 433 Programming Language Technologies and Paradigms Spring 2013 Introduction to Locks Intrinsic Locks Atomic-looking operations Resources created for sequential code make certain assumptions, a large one being that a method is an atomic thing. Consider how i++ might appear at a low level: Load i into register R1 Increment register R1 Store register R1 back to i What happens if i is a shared value and the above gets swapped out of the CPU in the middle of its execution?

Programming Invariants When implementing classes, it is common to assume that methods are atomic. A class invariant might not be upheld within a method, but by the time the method completes its work, the invariant is still true. With concurrent execution, an object s method could be paused in the middle, leaving that object in an invalid state as far as the rest of the world is concerned. Safe access of a thread-unsafe resource. Consider the Java class LinkedList<T> and its add() and poll() methods. In sequential programs a LinkedList object s add() or poll() would always complete before that object could be used to invoke anything else. In concurrent programs, a shared LinkedList object could have multiple threads invoking either of these methods and those calls could be swapped out of the CPU in the middle of their execution. The results are unpredictable!

Divide and Conquer Imagine taking a range of numbers that you wanted to check for primality and partitioning them among several threads, each of which does the following to its part of the question to add primes to a LinkedList object called listofprimes: for (int i=rangestart; i<=rangeend; i++) { if (isprime(i)) { listofprimes.add(i); Since that LinkedList is not thread-safe, the contents of listofprimes is unpredictable. Locks We can associate locks with things such as objects and following a programming paradigm where we always obtain a lock on an object before we invoke and operating that access it. This would essentially make each method call atomic. We could do this at a more granular level and restrict our use of locks for when the method might cause an invariant to be untrue in the middle, or when the method is accessing a value whose invariant might be untrue in the middle of some other method call. There are many locking mechanisms. We will start with intrinsic locks.

Intrinsic Locks Every object in Java has an intrinsic lock (sometimes called a monitor lock) with it. The syntax contains an implied lock and unlock for it. The body of the synchronized block will not be entered until the lock has been acquired. The lock will be released on exiting the body of that block (even if you do a return from the middle of it). for (int i=rangestart; i<=rangeend; i++) { if (isprime(i)) { synchronized(listofprimes) { listofprimes.add(i); Intrinsic Locks Limitation One limitation on this type of lock is that there is no way to simply attempt to acquire a lock. If you ask for a lock and can not acquire it, then you will block until you do.

Class-Level Locks If you wanted to have atomic methods in a class, you could mark each of them with the synchronized keyword. This has the same general effect as surrounding the body of the method with synchronized(this). Note that since intrinsic locks are what is called reentrant you could have a synchronized method call another synchronized method and there won t be a problem with deadlock. Once a thread acquires an intrinsic lock, it can obtain additional holds on it. It will keep track of the number of times it has acquired it and will not release it to the world until it releases its hold on it that many times. No need to lock immutable objects In reality, locking is only an issue with mutable objects (or shared primitives) and not with immutable objects. With an immutable object, the only time that values change are when the constructor is called. The constructor has to be called when you first create the object. If the object is to be shared, it has to be created before the threads that share it are launched. Therefore, not even the constructor is something that will cause problems if it is swapped out of the CPU in the middle of its execution.

Copyright 2012-2013 : Evan Golub