Synchronization Lecture 23 – Fall 2017.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?
CS 11 java track: lecture 7 This week: Web tutorial:
Threading Part 4 CS221 – 4/27/09. The Final Date: 5/7 Time: 6pm Duration: 1hr 50mins Location: EPS 103 Bring: 1 sheet of paper, filled both sides with.
Threading Part 3 CS221 – 4/24/09. Teacher Survey Fill out the survey in next week’s lab You will be asked to assess: – The Course – The Teacher – The.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Multithreading.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Lecture 21 – CS2110 – Fall 2010 RACE CONDITIONS AND SYNCHRONIZATION.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Producer-Consumer Problem The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.bufferqueue.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Lecture 23 – CS2110 – Fall 2015 RACE CONDITIONS & SYNCHRONIZATION.
Lecture 5 Page 1 CS 111 Summer 2013 Bounded Buffers A higher level abstraction than shared domains or simple messages But not quite as high level as RPC.
pThread synchronization
Distributed and Parallel Processing George Wells.
Tutorial 2: Homework 1 and Project 1
Race Conditions & Synchronization
Race Conditions & Synchronization
A brief intro to: Parallelism, Threads, and Concurrency
Multithreading / Concurrency
Multi Threading.
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Multiple Writers and Races
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Atomicity CS 2110 – Fall 2017.
Threads, Concurrency, and Parallelism
Lecture 24 Concurrency 2 (D&D 23) Date.
Thread synchronization
Threads and Concurrency in Java: Part 2
Synchronization Lecture 24 – Spring April 2018
Synchronization Lecture 24 – Fall 2018.
Condition Variables and Producer/Consumer
Race Conditions and Synchronization
Condition Variables and Producer/Consumer
Thread Synchronization
COT 5611 Operating Systems Design Principles Spring 2014
COP 4600 Operating Systems Fall 2010
More on Thread Safety CSE451 Andrew Whitaker.
Race Conditions & Synchronization
Producer-Consumer Problem
Shared Memory Programming
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
Synchronization Lecture 24 – Fall 2018.
Dr. Mustafa Cem Kasapbaşı
Java Concurrency 17-Jan-19.
Concurrency: Mutual Exclusion and Process Synchronization
Java Concurrency.
CSE 153 Design of Operating Systems Winter 2019
Java Concurrency.
Threads and Multithreading
Synchronization Lecture 24 – Fall 2018.
Java Concurrency 29-May-19.
Software Engineering and Architecture
Don Porter Portions courtesy Emmett Witchel
some important concepts
Synchronization Lecture 24 – Fall 2018.
Synchronization and liveness
Presentation transcript:

Synchronization Lecture 23 – Fall 2017

Announcements A8 released today, Due: 11/21 Prelim 2 is in one week Late deadline is after Thanksgiving You can use your A6/A7 solutions or ours A7 correctness scores have been posted Next week's recitation will focus on A8 Prelim 2 is in one week Deadline for conflicts is today Review session on Sunday 11/14

Concurrent Programs A thread or thread of execution is a sequential stream of computational work. Concurrency is about controlling access by multiple threads to shared resources.

Race Conditions Thread 2 Thread 1 Initially, i = 0 Load 0 from memory tmp = load i; Load 0 from memory Load 0 from memory tmp = load i; tmp = tmp + 1; store tmp to i; Store 1 to memory tmp = tmp + 1; store tmp to i; Store 1 to memory time Finally, i = 1

Race Conditions A race condition is a situation in which the result of executing two or more processes in parallel can depend on the relative timing of the execution of the processes. A race condition can arises if two threads try to read and write the same data. Often occurs if a thread might see the data in the middle of an update (in a "inconsistent stare”) Can lead to subtle and hard-to-fix bugs Solved by synchronization Today: understand java constructs for synchronization

An Example: Bounded Buffers finite capacity (e.g. 20 loaves) implemented as a queue Threads A: produce loaves of bread and put them in the queue Threads B: consume loaves by taking them off the queue

An Example: Bounded Buffers finite capacity (e.g. 20 loaves) implemented as a queue Separation of concerns: How do you implement a queue in an array? How do you implement a bounded buffer, which allows producers to add to it and consumers to take things from it, all in parallel? Threads A: produce loaves of bread and put them in the queue Threads B: consume loaves by taking them off the queue

ArrayQueue Array b[0..5] 0 1 2 3 4 5 b.length b 5 3 6 2 4 put values 5 3 6 2 4 into queue

ArrayQueue Array b[0..5] 0 1 2 3 4 5 b.length b 5 3 6 2 4 10 Array b[0..5] 0 1 2 3 4 5 b.length b 5 3 6 2 4 put values 5 3 6 2 4 into queue get, get, get

ArrayQueue Array b[0..5] 0 1 2 3 4 5 b.length b 3 5 2 4 1 11 Array b[0..5] 0 1 2 3 4 5 b.length b 3 5 2 4 1 Values wrap around!! put values 5 3 6 2 4 into queue get, get, get put values 1 3 5

ArrayQueue h 0 1 2 3 4 5 b.length b 3 5 2 4 1 Values wrap around!! 12 h 0 1 2 3 4 5 b.length b 3 5 2 4 1 Values wrap around!! int[] b; // The array elements of the queue are in int h; // location of head, 0 <= h < b.length int n; // number of elements currently in queue /** Pre: there is space */ public void put(int v){ b[(h+n) % b.length]= v; n= n+1; } /** Pre: not empty */ public int get(){ int v= b[h]; h= (h+1) % b.length n= n-1; return v; }

Bounded Buffer 13 /** An instance maintains a bounded buffer of fixed size */ class BoundedBuffer<E> { ArrayQueue<E> aq; /** Put v into the bounded buffer.*/ public void produce(E v) { if(!aq.isFull()){ aq.put(v) }; } /** Consume v from the bounded buffer.*/ public E consume() { aq.isEmpty() ? return null : return aq.get();

Synchronized Blocks a.k.a. locks or mutual exclusion synchronized (q) { if (!q.isEmpty()) { q.remove(); } At most one consumer thread can be trying to remove something from the queue at a time. While this method is executing the synchronized block, object aq is locked. No other thread can obtain the lock. We need a way to prohibit other threads from using AQ aq while the code to change aq is being executed. For this purpose, we use Java keyword synchronize

Synchronized Blocks public void produce(E v) { synchronized(aq){ if(!aq.isFull()){ aq.put(v); } } public void produce(E v) { synchronized(this){ if(!aq.isFull()){ aq.put(v); } } You can synchronize (lock) any object, including this.

Synchronized Methods public void produce(E v) { synchronized(this){ if(!aq.isFull()){ aq.put(v); } } You can synchronize (lock) any object, including this. public synchronized void produce(E v) { if(!aq.isFull()){ aq.put(v); } } Or you can synchronize methods This is the same as wraping the entire method implementation in a synchronized(this) block

Bounded Buffer What happens of aq is full? 17 /** An instance maintains a bounded buffer of fixed size */ class BoundedBuffer<E> { ArrayQueue<E> aq; /** Put v into the bounded buffer.*/ public synchronized void produce(E v) { if(!aq.isFull()){ aq.put(v); } } /** Consume v from the bounded buffer.*/ public synchronized E consume() { aq.isEmpty() ? return null : return aq.get(); What happens of aq is full? We want to wait until it becomes non-full —until there is a place to put v. Somebody has to buy a loaf of bread before we can put more bread on the shelf.

wait() is a method defined in Object For every synchronized object sobj, Java maintains: locklist: a list of threads that are waiting to obtain the lock on sobj waitlist: a list of threads that had the lock but executed wait() e.g., because they couldn't proceed wait() is a method defined in Object

Wait() need while loop (not if statement) to prevent race conditions 19 /** An instance maintains a bounded buffer of fixed size */ class BoundedBuffer<E> { ArrayQueue<E> aq; /** Put v into the bounded buffer.*/ public synchronized void produce(E v) { while(aq.isFull()){ try { wait(); } catch(InterruptedException e){} } aq.put(v); ... need while loop (not if statement) to prevent race conditions puts thread on the wait list what wait does why while loop what interrupt is threads can be interrupted if this happens just continue notifyAll()

notify() and notifyAll() notify() and notifyAll() are methods defined in Object notify() moves one thread from the waitlist to the locklist Note: which thread is moved is arbitrary notifyAll() moves all the threads on the waitlist to the locklist

notify() and notifyAll() 21 /** An instance maintains a bounded buffer of fixed size */ class BoundedBuffer<E> { ArrayQueue<E> aq; /** Put v into the bounded buffer.*/ public synchronized void produce(E v) { while(aq.isFull()){ try { wait(); } catch(InterruptedException e){} } aq.put(v); ... discuss: why notifyAll instead of notify Then look at consumer method in code notifyAll()

WHY use of notify() may hang. Two sets: 1. Runnable: threads waiting to get lock. 2. Waiting: threads waiting to be notified 22 Work with a bounded buffer of length 1. 1. Consumer W gets lock, wants White bread, finds buffer empty, and wait()s: is put in set 2. 2. Consumer R gets lock, wants Rye bread, finds buffer empty, wait()s: is put in set 2. 3. Producer gets lock, puts Rye in the buffer, does notify(), gives up lock. 4. The notify() causes one waiting thread to be moved from set 2 to set 1. Choose W. 5. No one has lock, so one Runnable thread, W, is given lock. W wants white, not rye, so wait()s: is put in set 2. 6. Producer gets lock, finds buffer full, wait()s: is put in set 2. All 3 threads are waiting in set 2. Nothing more happens.

Should one use notify() or notifyAll() 23 But suppose there are two kinds of bread on the shelf —and one still picks the head of the queue, if it’s the right kind of bread. Using notify() can lead to a situation in which no one can make progress. notifyAll() always works; you need to write documentation if you optimize by using notify()

Using Concurrent Collections... 24 Java has a bunch of classes to make synchronization easier. It has synchronized versions of some of the Collections classes It has an Atomic counter.

From spec for HashSet 25 … this implementation is not synchronized. If multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be "wrapped" using method Collections.synchronizedSet This is best done at creation time, to prevent accidental unsynchronized access to the set: Set s = Collections.synchronizedSet(new HashSet(...));

Race Conditions Thread 2 Thread 1 Initially, i = 0 Load 0 from memory tmp = load i; Load 0 from memory Load 0 from memory tmp = load i; tmp = tmp + 1; store tmp to i; Store 1 to memory tmp = tmp + 1; store tmp to i; Store 1 to memory time Finally, i = 1

Using Concurrent Collections... 27 import java.util.concurrent.atomic.*;   public class Counter { private static AtomicInteger counter; public Counter() { counter= new AtomicInteger(0); } public static int getCount() { return counter.getAndIncrement();

Summary 28 Use of multiple processes and multiple threads within each process can exploit concurrency may be real (multicore) or virtual (an illusion) Be careful when using threads: synchronize shared memory to avoid race conditions avoid deadlock Even with proper locking concurrent programs can have other problems such as “livelock” Serious treatment of concurrency is a complex topic (covered in more detail in cs3410 and cs4410) Nice tutorial at http://docs.oracle.com/javase/tutorial/essential/concurrency/index. html