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

Similar documents
Chapter 32 Multithreading and Parallel Programming

Lecture 8: September 30

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

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

Concurrent Programming using Threads

Threads Questions Important Questions

CMSC 330: Organization of Programming Languages

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

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

Lecture 10 State dependency

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

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

CS11 Java. Fall Lecture 7

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

Synchronization in Java

Threads and Parallelism in Java

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

Java Threads. COMP 585 Noteset #2 1

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

Concurrency in Java Prof. Stephen A. Edwards

Synchronization Lecture 24 Fall 2018

Introduction to Java Threads

CMSC 132: Object-Oriented Programming II

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

Multiple Inheritance. Computer object can be viewed as

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

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

Monitors; Software Transactional Memory

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

CSCD 330 Network Programming

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

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Performance Throughput Utilization of system resources

Java Threads. Introduction to Java Threads

Concurrent Programming

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

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

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

Programming Language Concepts: Lecture 11

Introduction to OS Synchronization MOS 2.3

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

Monitors; Software Transactional Memory

What is a thread anyway?

IT 540 Operating Systems ECE519 Advanced Operating Systems

Models of concurrency & synchronization algorithms

Synchronization in Concurrent Programming. Amit Gupta

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

Lecture 29: Java s synchronized statement

Dealing with Issues for Interprocess Communication

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

JAVA CONCURRENCY FRAMEWORK. Kaushik Kanetkar

UNIX Input/Output Buffering

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Synchronization I. Jo, Heeseung

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

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

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

What's wrong with Semaphores?

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

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

CSCD 330 Network Programming

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

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

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects

Synchronization Lecture 23 Fall 2017

Animation Part 2: MoveableShape interface & Multithreading

Chapter 19 Multithreading

Concurrent Processes Rab Nawaz Jadoon

Synchronization

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

CSE 153 Design of Operating Systems

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

G52CON: Concepts of Concurrency

Concurrency Race Conditions and Deadlocks

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

Process Management And Synchronization

Results of prelim 2 on Piazza

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

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

Only one thread can own a specific monitor

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

Component-Based Software Engineering

CS 159: Parallel Processing

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Faculty of Computers & Information Computer Science Department

Advanced Concepts of Programming

Interprocess Communication By: Kaushik Vaghani

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

Chapter 6: Process Synchronization

CS18000: Programming I

Page 1. CS162 Operating Systems and Systems Programming Lecture 8. Readers-Writers Language Support for Synchronization

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

Chapter 6: Process Synchronization

Module - 4 Multi-Threaded Programming

Sharing is the Key. Lecture 25: Parallelism. Canonical Example. Bad Interleavings. Common to have: CS 62 Fall 2016 Kim Bruce & Peter Mawhorter

CMSC 330: Organization of Programming Languages

Programming Language Concepts: Lecture 13

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

Shared-Memory and Multithread Programming

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

Transcription:

Thread communication and synchronization There are two main aspects to Outline for Lecture 19 multithreaded programming in Java: I. Thread synchronization. thread lifecycle, and thread synchronization. Thread lifecycle, which we considered last lecture, is usually the easier part. Thread synchronization involves challenges like avoiding deadlock and starvation, preventing data corruption, and sharing resources effectively. II. Need for synchronization synchronized methods Guarded actions wait and notify Paradigms for use Indivisibility and monitors Monitor queues III. Synchronized statements vs. synchronized methods Need for synchronization: If threads comprise parts of our software systems, then they must communicate. IV. Deadlock V. Interrupting threads For two or more independent tasks to communicate, there must be: A medium for information exchange Synchronization between the tasks In an OO system, typically a shared object plays both roles. Suppose we have two threads in our bank program, each representing a separate teller. One is processing a check deposited in the customer s account, while the other is processing a withdrawal by the customer. Here is a set of actions that might occur: Lecture 19 Object-Oriented Languages and Systems 1

p 1 : balance :=balance +10; p 2 : balance := balance 5; (1a) R1 := balance; (2a) R2 := balance; (1b) R1 := R1 +10; (2b) R2 := R2 5; (1c) balance := R1; (2c) balance := R2; Assuming that the initial balance is 100, what are the possible ending balances? Clearly, only one of these answers is correct. To prevent this situation, there must be some way to prevent one transaction from starting while the other is in progress. Java provides two ways to control concurrency: synchronized methods, and synchronized statements. That is, the responsibility to protect an object from corruption can be placed on the threads via techniques like critical sections. Or alternatively, responsibility can be associated with the object itself. synchronized methods: Let us first consider the case of synchronized methods. Any method can be marked as synchronized. If so, it is always executed in its entirety (unless explicitly suspended, for example, via a wait) before any other synchronized method of the object is allowed to start. When a thread is executing a synchronized method on an object, the object is locked. Another thread invoking a synchronized method on the same object will block until the lock is released; in other words, until 2002 Edward F. Gehringer CSC 517/ECE 517 Lecture Notes, Fall 2002 2

Here is an example of an Account class for a multithreaded environment. class Account { private String name; private float balance; public Account(float initialbalance) { balance = initialbalance; public synchronized float balance() { return balance; public synchronized void balance(float newbalance) { balance := newbalance; public synchronized void deposit(float amount) { balance += amount; public synchronized void withdraw(float amount) { balance -= amount; Note that the Account constructor didn t need to be synchronized. Why? Also, note that the access methods are synchronized. This is another reason to write getters and setters instead of making the instance variable public. If a subclass overrides a synchronized method, the overriding method can be synchronized or not. The overridden method is still synchronized, however, so if the code invokes super method_name, the object will become locked. If all methods in a class were marked synchronized, then an object of that class would behave like a monitor. Lecture 19 Object-Oriented Languages and Systems 3

Informally, objects of a class that defines synchronized methods are said to be using a monitor. Note that each instance of a class has its own monitor. In Java, only a subset of the interfaces on an object (its synchronized methods) are protected. Each object (java.lang.object) has an associated synchronization lock. A thread invoking a synchronized method must acquire the object s lock upon entering and release it upon leaving the method. A synchronized method, once invoked will run to completion before any other thread gets a chance to run any synchronized method of the same object. To investigate synchronized methods (and the subsequent guarded actions), let s consider a bounded buffer. The bounded buffer is implemented using a fixed-sized array. We need to have methods to insert an object, to delete an entry, and allow simple indexed access to support traversal. Surely, a BoundedBuffer object should only be doing one of these things at a time, so each of these should be marked as synchronized. class BoundedBuffer { static private final int CAPACITY = 100; private String items[] = new String[CAPACITY]; private int len = 0; public synchronized void add(string obj) { if (len >= CAPACITY) return; items[len] = obj; ++len; public synchronized int size() { return len; public synchronized String nth(int i) { if (i >= len) return null; return items[i]; 2002 Edward F. Gehringer CSC 517/ECE 517 Lecture Notes, Fall 2002 4

public synchronized void remove(string obj) { for (int i = 0; i < len; ++i) { if (items[i].equals(obj)) { items[i] = items[len-1]; items[len-1] = null; --len; return; Code for a synchronized method may call itself or another synchronized method of the same object. Thus, the sychronization lock is per object, but is held per thread. What does this mean? Guarded actions In the above code, the add method does nothing at all if the bounded buffer is full. In single-threaded code, you could raise some sort of exception in case of a full or empty buffer. In concurrent code, however, it is also possible to wait until a subsequent remove is performed by some other thread. This can easily be achieved by using wait() and notify(). Recall that wait() and notify() are defined in class Object. They may only be called in synchronized methods. Calls to wait and notify are normally self-calls, made within public synchronized methods of an object. A call to wait() can be made only by the thread that owns the object s monitor. Lecture 19 Object-Oriented Languages and Systems 5

The thread releases ownership of the monitor, and waits until another thread notifies threads waiting on this object s monitor to wake up. It can do that either through a call to (What is the difference between the two?) The thread then waits till it re-obtains ownership of the monitor, and then resumes execution. Like sleep(), wait(long) can also be called with one parameter, a timeout period in milliseconds. In this case, the thread waits either until it is notified to wake up, or till the timeout period expires. Again, the thread waits until it can regain ownership of the monitor. These methods form only the raw basis for synchronization. To return to the bounded-buffer example, we could rewrite the methods as follows: public synchronized void add(string obj) { while (len >= capacity) { try { wait(); catch(interruptedexception e) { items[len] = obj; ++len; public synchronized void remove(string obj) { for (int i = 0; i < len; ++i) { if (items[i].equals(obj)) { items[i] = items[len-1]; items[len-1] = null; --len; notify(); return; 2002 Edward F. Gehringer CSC 517/ECE 517 Lecture Notes, Fall 2002 6

As a general rule, condition waits should always be placed in while loops, as in the code above. The paradigm goes like this: synchronized void dowhencondition() { while (! condition ) wait(); // Do what needs to be done when condition holds This is because when the action is resumed, the waiting task doesn t know if the condition is actually true; it only knows that it has been woken up. What could have happened? Why is it safe to assume the condition is true if a while loop is used? Note that wait() atomically releases the lock on the object. This means that immediately after the wait() is executed, the lock is released. Nothing can intervene. If these actions weren t atomic, a race condition could arise. Suppose a notify came along after, but before Then the notify would have no effect on the thread. And the thread would not reacquire the lock, raising the specter of consistency problems. The paradigm for notifying a thread goes something like this: synchronized void changecondition() { Lecture 19 Object-Oriented Languages and Systems 7

// Change some value used in a condition test notify(); More than one thread may be waiting on the same object. When a notify() is invoked, it wakes up the thread that has been waiting the longest. If a notifyall() is invoked, all threads waiting on this object wake up. Monitor queues Consider this example. There is a random object manipulated via synchronized methods. Thread 1 enters a synchronized method, and subsequently performs a wait. Thread 2 enters a synchronized method, and subsequently performs a wait. There is now a wait queue with 1 in front of 2. Thread 3 enters a synchronized method (and then gets switched out by the scheduler). Thread 4 blocks trying to enter a synchronized method. Why? Wait queue 1 Wait queue 1 2 Monitor Wait queue 3 1 2 4 5 Monitor? Wait queue Thread 5 blocks trying to enter a synchronized method (can t acquire the lock). 3 1 2 Thread 3 resumes, and issues a notifyall(). What is the result? 2002 Edward F. Gehringer CSC 517/ECE 517 Lecture Notes, Fall 2002 8

Thread 3 exits the synchronized methods. Thread 1 gets the lock and subsequently exits the synchronized method. synchronized statement In addition to synchronized methods, Java also has a synchronized statement. This statement specifies an object to be locked, and a statement to execute when the lock is obtained. The syntax is synchronized ( expression ) statement to execute The expression must evaluate to an object. Usually it is just the name of an object, e.g., myobject. Usually there is more than one statement to execute, so a block is used instead ({ statement1 ; statement2 ;, statement n A lock is acquired on myobject and then the block is executed. The lock is released upon exiting the block. Here is code that updates both words of a two-element array indivisibly: Lecture 19 Object-Oriented Languages and Systems 9

int two [] = new int[2]; synchronized (two) { if (two[1] == 2147483647) {two[0] += 1; two[1] = 0; else two[1] += 1; This construct allows any Java object to act as a binary semaphore. There is no requirement that the object of a synchronized statement actually be used in the body of the statement. In fact, we could declare an Object just to control access to a critical section. Thus, in Java, there are two ways to control access to critical sections synchronized methods, and synchronized statements. Which is better? It depends on the situation. For example, suppose you are presented with a class that works only for sequential code, not for multithreaded code. You want to extend it to work in a multithreaded environment. You can subclass this class, using synchronized methods to override appropriate methods, then forward method calls using the super construct, or don t subclass, but rather, take care to use objects of this class only within a synchronized statement. Which is better? 2002 Edward F. Gehringer CSC 517/ECE 517 Lecture Notes, Fall 2002 10

Deadlock A well known hazard of monitors is the nested monitor-call problem. It goes like this: A process in one monitor M1 calls a procedure in another monitor M2. The mutual exclusion in M1 is not released while M2 is executing. This has two implications: Any process calling M1 will be blocked outside M1 during this period. If the process enters a condition queue in M2, deadlock may occur. M1 M2 Let s look at an example in Java. A class called Friend has synchronized methods are_you_there and im_here. The are_you_there method immediately invokes im_here on its parameter. Let s suppose irving and edna are instances of Friend. Thread 1 Thread 2 1. irving.are_you_there(edna) 2. edna.are_you_there(irving) 3. irving.im_here 4. edna.im_here Which locks are held at the end of each step? Lecture 19 Object-Oriented Languages and Systems 11

After Step 1, Thread 1 has a lock on After Step 2, Thread 2 has a lock on After Step 3, After Step 4, Now, neither thread can proceed until the other releases a lock. So deadlock has occurred. Of course, we could get lucky. There d be no deadlock if one thread completed its call to before the other started its. How can we fix this problem? The simplest fix would be to use a synchronized statement instead of synchronized methods. The two methods could synchronize on a single (lock) object shared by all Friends. What would be the disadvantage of this approach? More sophisticated strategies could prevent deadlock without incurring this disadvantage. Interrupting threads It is possible to interrupt a thread. This doesn t really stop the thread from doing what it s doing. Rather, it sets a flag that can be tested later by the thread. For example, a display-update loop might need to perform a database transaction to get the information it needs to display. 2002 Edward F. Gehringer CSC 517/ECE 517 Lecture Notes, Fall 2002 12

Suppose the user tries to stop the update thread. The display thread still doesn t want to stop until the database transaction completes. This can be implemented as follows: The user interface issues displayupdate.interrupt(). When a transaction completes, the display update always checks whether it has been interrupted, via isinterrupted(). If the display update has been interrupted, it stops (exits its update loop). Although interrupting a thread normally doesn t affect what it is doing, there are some exceptions. Methods like sleep and wait throw InterruptedExceptions if they are interrupted. Of course, these exceptions can be caught, and appropriate action taken. Lecture 19 Object-Oriented Languages and Systems 13