Java Monitor Objects: Synchronization (Part 1)

Similar documents
Overview of Java Threads (Part 3)

Java ReentrantLock (Part 1)

Java Semaphore (Part 1)

The Java FutureTask. Douglas C. Schmidt

Java Barrier Synchronizers: CountDownLatch (Part 1)

Benefits of Concurrency in Java & Android: Program Structure

What is a thread anyway?

Java 8 Parallel Stream Internals (Part 2)

Java 8 Parallel ImageStreamGang Example (Part 3)

The Java ExecutorService (Part 1)

Introduction to Locks. Intrinsic Locks

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

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

Overview of Java Threads (Part 2)

Overview of Java s Support for Polymorphism

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

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

The Java Executor (Part 1)

Java Barrier Synchronizers: CyclicBarrier (Part 1)

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

CMSC 330: Organization of Programming Languages

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

CS 351 Design of Large Programs Threads and Concurrency

Process Synchronization

wait with priority An enhanced version of the wait operation accepts an optional priority argument:

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

Concurrent Programming

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

Multi-threaded programming in Java

Java Threads and intrinsic locks

Models of concurrency & synchronization algorithms

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

CMSC 330: Organization of Programming Languages

Synchronization in Java

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Concurrent Programming using Threads

Inheritance. Transitivity

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Atomicity CS 2110 Fall 2017

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

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

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

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

Class, Variable, Constructor, Object, Method Questions

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

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

Declarations and Access Control SCJP tips

Overview of Advanced Java 8 CompletableFuture Features (Part 3)

Overview of C++: Part 1

Sharing Objects Ch. 3

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

INHERITANCE. Spring 2019

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

EMBEDDED SYSTEMS PROGRAMMING More About Languages

Parallel linked lists

RACE CONDITIONS AND SYNCHRONIZATION

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

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

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

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

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

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

Overview of Activities

Introduction to Programming Using Java (98-388)

Threads Synchronization

Synchronization SPL/2010 SPL/20 1

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

G52CON: Concepts of Concurrency

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

3/25/14. Lecture 25: Concurrency. + Today. n Reading. n P&C Section 6. n Objectives. n Concurrency

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

+ Today. Lecture 26: Concurrency 3/31/14. n Reading. n Objectives. n Announcements. n P&C Section 7. n Race conditions.

Threads Questions Important Questions

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

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

Chapter 32 Multithreading and Parallel Programming

The New Java Technology Memory Model

CHAPTER 6: PROCESS SYNCHRONIZATION

Memory system behavior: volatile variables. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 6. Volatile variables and concurrency

CS 159: Parallel Processing

CS11 Java. Fall Lecture 7

Object Oriented Issues in VDM++

Threads and Java Memory Model

Note: in this document we use process and thread interchangeably.

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

Problems with Concurrency. February 19, 2014

What is a Thread? Why Multicore? What is a Thread? But a fast computer runs hot THREADS AND CONCURRENCY. Concurrency (aka Multitasking)

Synchronization Principles

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

User Space Multithreading. Computer Science, University of Warwick

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

JAVA MOCK TEST JAVA MOCK TEST II

TEST CONCURRENT PROGRAMMING. code: date: 19 June 2015 time:

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

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

Infrastructure Middleware (Part 1): Hardware Abstraction Layer (HAL)

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

Transcription:

Java Monitor Objects: Synchronization (Part 1) Douglas C. Schmidt d.schmidt@vanderbilt.edu www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Learning Objectives in this Part of the Lesson Recognize the synchronized methods/statements provided by Java build-in monitor objects to support mutual exclusion Producer offer() Monitor Object synchronized m1() synchronized m2() poll() Consumer <<contains>> 1 <<contains>> 1 Wait Queue Entrance Queue Mutual exclusion is used to protect shared state from corruption due to concurrent 2 access by multiple threads

Java Synchronized Methods 3

The BusySynchronizedQueue class showcases Java built-in synchronization mechanisms Java Synchronized Methods class BusySynchronizedQueue<E> implements BoundedQueue<E> { private LinkedList<E> mlist; private int mcapacity; BusySynchronizedQueue(int capacity) { mlist = new LinkedList<E>(); mcapacity = capacity; See github.com/douglascraigschmidt/posa/tree/ master/ex/m3/queues/busysynchronizedqueue 4

The BusySynchronizedQueue class showcases Java built-in synchronization mechanisms Java Synchronized Methods class BusySynchronizedQueue<E> implements BoundedQueue<E> { private LinkedList<E> mlist; private int mcapacity; BusySynchronizedQueue(int capacity){ mlist = new LinkedList<E>(); mcapacity = capacity; 5

The BusySynchronizedQueue class showcases Java built-in synchronization mechanisms Java Synchronized Methods class BusySynchronizedQueue<E> implements BoundedQueue<E> { private LinkedList<E> mlist; private int mcapacity; This internal state must be protected against race conditions BusySynchronizedQueue(int capacity){ mlist = new LinkedList<E>(); mcapacity = capacity; 6

The BusySynchronizedQueue class showcases Java built-in synchronization mechanisms The constructor initializes the internal state Java Synchronized Methods class BusySynchronizedQueue<E> implements BoundedQueue<E> { private LinkedList<E> mlist; private int mcapacity; BusySynchronizedQueue(int capacity){ mlist = new LinkedList<E>(); mcapacity = capacity; 7

Methods in a built-in monitor object can be marked with the synchronized keyword Java Synchronized Methods class BusySynchronizedQueue<E> implements BoundedQueue<E> { public synchronized boolean offer(e e) { public synchronized E poll() { public synchronized boolean isempty() { See docs.oracle.com/javase/tutorial/ essential/concurrency/syncmeth.html 8

Methods in a built-in monitor object can be marked with the synchronized keyword A synchronized method is serialized wrt any other synchronized method in an object Java Synchronized Methods class BusySynchronizedQueue<E> implements BoundedQueue<E> { public synchronized boolean offer(e e) { public synchronized E poll() { public synchronized boolean isempty() { See lesson on Java ReentrantLock 9

Methods in a built-in monitor object can be marked with the synchronized keyword A synchronized method is serialized wrt any other synchronized method in an object When used in the method declaration, the entire body of the method is serialized Java Synchronized Methods class BusySynchronizedQueue<E> implements BoundedQueue<E> { public synchronized boolean offer(e e) { if (!isfull()) { mlist.add(e); return true; else return false; public synchronized E poll() { return mlist.poll(); public synchronized boolean isempty() { return mlist.size() == 0; 10

The synchronized keyword is not considered to be part of a method's signature Java Synchronized Methods class BusySynchronizedQueue<E> implements BoundedQueue<E> { public synchronized boolean offer(e e) { public synchronized E poll() { Synchronization is considered to be an implementation detail public synchronized boolean isempty() { See gee.cs.oswego.edu/dl/cpj/ mechanics.html#synchronization 11

The synchronized keyword is not considered to be part of a method's signature synchronized is not inherited when subclasses override superclass methods Java Synchronized Methods class SynchronizedQueue<E> extends BusySynchronizedQueue<E>{ public boolean offer(e e) { public E poll() { These methods will not be synchronized unless the implementation explicitly synchronizes them public boolean isempty() { 12

Java Synchronized Methods Pros of synchronized methods Synchronized methods can be identified by examining the method interfaces The syntax is compact i.e., the code is more legible The method is the unit of synchronization See stackoverflow.com/questions/574240/is-there-an-advantage-to-usea-synchronized-method-instead-of-a-synchronized-blo/574525#574525 13

Cons of synchronized methods Synchronizes to the intrinsic lock (this), so it is possible for other objects to synchronize with it too The granularity of locking is coarse-grained i.e., locking is a a perobject/per-method basis Java Synchronized Methods See stackoverflow.com/questions/574240/is-there-an-advantage-to-usea-synchronized-method-instead-of-a-synchronized-blo/574525#574525 14

End of Java Monitor Objects: Synchronization (Part 1) 15

Java Monitor Objects: Synchronization (Part 2) Douglas C. Schmidt d.schmidt@vanderbilt.edu www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Learning Objectives in this Part of the Lesson Recognize the synchronized methods/statements provided by Java build-in monitor objects to support mutual exclusion Producer offer() m1() m2() Monitor Object poll() Consumer <<contains>> 1 <<contains>> 1 Wait Queue Entrance Queue void m1() { synchronized(this) { Mutual exclusion is used to protect shared state from corruption due to concurrent 2 access by multiple threads

Java Synchronized Statements 3

Synchronized methods incur several constraints Java Synchronized Statements 4

Synchronized methods incur several constraints, e.g. They can yield excessive overhead due to coarsegrained serialization Java Synchronized Statements Thread 1 m1() m2() Synchronized A Java Monitor Queue Object synchronized m1() synchronized m2() Thread 2 Synchronization occurs at the method level <<contains>> Wait Queue wait() notify() notifyall() 1 1 <<contains>> Entrance Queue 5

Synchronized methods incur several constraints, e.g. Java Synchronized Statements They can yield excessive overhead due to coarsegrained serialization Always synchronizes on the implicit lock (this) May be a source of contention Thread 1 m1() <<contains>> m2() Synchronized A Java Monitor Queue Object synchronized m1() synchronized m2() 1 1 <<contains>> Thread 2 Wait Queue wait() notify() notifyall() Entrance Queue 6

e.g., consider the Java Exchanger class Java Synchronized Statements Defines a synchronization point where threads can pair & swap elements within pairs public class Exchanger<V> { private synchronized void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; if (a[index] == null) a[index] = newslot; private volatile Slot[] arena = new Slot[CAPACITY]; See grepcode.com/file/repository.grepcode.com/java/root/ 7 jdk/openjdk/6-b14/java/util/concurrent/exchanger.java

Java Synchronized Statements e.g., consider the Java Exchanger class One approach synchronizes at the method level public class Exchanger<V> { private synchronized void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; if (a[index] == null) a[index] = newslot; private volatile Slot[] arena = new Slot[CAPACITY]; 8

Java Synchronized Statements e.g., consider the Java Exchanger class One approach synchronizes at the method level public class Exchanger<V> { private synchronized void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; if (a[index] == null) a[index] = newslot; Lazily create slot if this is the first time it s accessed private volatile Slot[] arena = new Slot[CAPACITY]; 9

Java Synchronized Statements e.g., consider the Java Exchanger class One approach synchronizes at the method level Another approach synchronizes individual statements public class Exchanger<V> { private void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; synchronized (this) { if (a[index] == null) a[index] = newslot; private volatile Slot[] arena = new Slot[CAPACITY]; See docs.oracle.com/javase/tutorial/ 10 essential/concurrency/locksync.html

Java Synchronized Statements e.g., consider the Java Exchanger class One approach synchronizes at the method level Another approach synchronizes individual statements public class Exchanger<V> { private void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; synchronized (this) { if (a[index] == null) a[index] = newslot; Synchronized statements are finer-grained than synchronized methods private volatile Slot[] arena = new Slot[CAPACITY]; 11

Java Synchronized Statements e.g., consider the Java Exchanger class One approach synchronizes at the method level Another approach synchronizes individual statements public class Exchanger<V> { private void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; synchronized (this) { if (a[index] == null) a[index] = newslot; Create slot outside of lock to narrow the synchronization region private volatile Slot[] arena = new Slot[CAPACITY]; 12

Java Synchronized Statements e.g., consider the Java Exchanger class One approach synchronizes at the method level Another approach synchronizes individual statements Intrinsic lock is often used to synchronize a statement public class Exchanger<V> { private void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; synchronized (this) { if (a[index] == null) a[index] = newslot; Only this statement is serialized via the intrinsic lock private volatile Slot[] arena = new Slot[CAPACITY]; 13

Java Synchronized Statements e.g., consider the Java Exchanger class One approach synchronizes at the method level Another approach synchronizes individual statements Intrinsic lock is often used to synchronize a statement Explicit lock synchronization can also be used public class Exchanger<V> { private void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; synchronized (a) { if (a[index] == null) a[index] = newslot; Can also synchronize using an explicit object private volatile Slot[] arena = new Slot[CAPACITY]; 14

Java Synchronized Statements e.g., consider the Java Exchanger class One approach synchronizes at the method level Another approach synchronizes individual statements Intrinsic lock is often used to synchronize a statement Explicit lock synchronization can also be used e.g., in situations where the intrinsic lock is too limited or too contended public class Exchanger<V> { private void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; synchronized (a) { if (a[index] == null) a[index] = newslot; Can also synchronize using an explicit object private volatile Slot[] arena = new Slot[CAPACITY]; See www.dre.vanderbilt.edu/~schmidt/ 15 PDF/specific-notification.pdf

More info is available in the online Java documentation Java Synchronized Statements See docs.oracle.com/javase/tutorial/ essential/concurrency/locksync.html 16

Java Synchronized Statements Pros of synchronized statements Allows a private field to be used as the synchronizer i.e., hides implementation details Synchronized statements can be found by searching references to a variable via the IDE See stackoverflow.com/questions/574240/is-there-an-advantage-to-usea-synchronized-method-instead-of-a-synchronized-blo/574525#574525 17

Java Synchronized Statements Cons of synchronized statements The syntax is more complicated i.e., code is harder to read See stackoverflow.com/questions/574240/is-there-an-advantage-to-usea-synchronized-method-instead-of-a-synchronized-blo/574525#574525 18

Implementing the Double-Checked Locking Pattern 19

Implementing the Double-Checked Locking Pattern Synchronized statements can be used to implement patterns like Double-Checked Locking public class Exchanger<V> { private void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; synchronized (a) { if (a[index] == null) a[index] = newslot; private Object doexchange() { final Slot slot = arena[index]; if (slot == null) // Lazily initialize slots createslot(index); private volatile Slot[] arena = new Slot[CAPACITY]; See en.wikipedia.org/wiki/ Double-checked_locking 20

Implementing the Double-Checked Locking Pattern Synchronized statements can be used to implement patterns like Double-Checked Locking Synchronization is done lazily when initialization is first performed public class Exchanger<V> { private void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; synchronized (a) { if (a[index] == null) a[index] = newslot; private Object doexchange() { final Slot slot = arena[index]; if (slot == null) // Lazily initialize slots createslot(index); private volatile Slot[] arena = new Slot[CAPACITY]; 21

Implementing the Double-Checked Locking Pattern Synchronized statements can be used to implement patterns like Double-Checked Locking Synchronization is done lazily when initialization is first performed public class Exchanger<V> { private void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; synchronized (a) { if (a[index] == null) a[index] = newslot; Double-Checked Locking optimization is done here private Object doexchange() { final Slot slot = arena[index]; if (slot == null) // Lazily initialize slots createslot(index); private volatile Slot[] arena = new Slot[CAPACITY]; 22

Implementing the Double-Checked Locking Pattern Synchronized statements can be used to implement patterns like Double-Checked Locking Synchronization is done lazily when initialization is first performed There s no need to synchronize this check since reference reads & writes are atomic public class Exchanger<V> { private void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; synchronized (a) { if (a[index] == null) a[index] = newslot; private Object doexchange() { final Slot slot = arena[index]; if (slot == null) // Lazily initialize slots createslot(index); private volatile Slot[] arena = new Slot[CAPACITY]; See docs.oracle.com/javase/specs/ 23 jls/se8/html/jls-17.html#jls-17.7

Implementing the Double-Checked Locking Pattern Synchronized statements can be used to implement patterns like Double-Checked Locking Synchronization is done lazily when initialization is first performed public class Exchanger<V> { private void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; synchronized (a) { if (a[index] == null) a[index] = newslot; A new slot is created only if the current slot is null private Object doexchange() { final Slot slot = arena[index]; if (slot == null) // Lazily initialize slots createslot(index); private volatile Slot[] arena = new Slot[CAPACITY]; 24

Implementing the Double-Checked Locking Pattern Synchronized statements can be used to implement patterns like Double-Checked Locking Synchronization is done lazily when initialization is first performed public class Exchanger<V> { private void createslot(int index){ final Slot newslot = new Slot(); final Slot[] a = arena; synchronized (a) { if (a[index] == null) a[index] = newslot; Only synchronize during initialize private Object doexchange() { final Slot slot = arena[index]; if (slot == null) // Lazily initialize slots createslot(index); private volatile Slot[] arena = new Slot[CAPACITY]; 25

End of Java Monitor Objects: Synchronization (Part 2) 26

Java Monitor Objects: Synchronization (Part 3) Douglas C. Schmidt d.schmidt@vanderbilt.edu www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Learning Objectives in this Part of the Lesson Recognize the synchronized methods/statements provided by Java build-in monitor objects to support mutual exclusion Understand how to fix the race conditions in the buggy concurrent Java app by using synchronized methods Producer offer() BusySynchronized Queue synchronized offer() synchronized poll() poll() Consumer <<contains>> 1 <<contains>> 1 Wait Queue Entrance Queue The use of synchronized methods only 2 provides a partial solution, however

Partial Solution Using Java Synchronized Methods 3

Partial Solution Using Java Synchronized Methods See en.wikipedia.org/wiki/ Crazy_Horse_Memorial 4

Partial Solution Using Java Synchronized Methods A concurrent producer/consumer app that passes messages via the class BusySynchronizedQueue See github.com/douglascraigschmidt/posa/tree/ master/ex/m3/queues/busysynchronizedqueue 5

Partial Solution Using Java Synchronized Methods The BusySynchronizedQueue is modeled on the Java ArrayBoundedQueue Producer offer() BusySynchronized Queue offer() poll() poll() Consumer See docs.oracle.com/javase/8/docs/api/ 6 java/util/concurrent/arrayboundedqueue.html

Partial Solution Using Java Synchronized Methods UML class diagram showing the design of the BusySynchronizedQueue See github.com/douglascraigschmidt/posa/tree/master/ ex/m3/queues/busysynchronizedqueue/app/src/main/ 7 java/edu/vandy/busysynchronizedqueue/model

Partial Solution Using Java Synchronized Methods UML sequence diagram of the BusySynchronizedQueue unit test : BusySynchronized QueueTest consumer : Thread producer : Thread busyqueue : Busy SynchronizedQueue main() new() new() new() start() run() start() run() offer( ") poll() See github.com/douglascraigschmidt/posa/tree/master/ex/m3/queues/ BusySynchronizedQueue/app/src/test/java/edu/vandy/busysynchronizedqueue 8

Partial Solution Using Java Synchronized Methods UML sequence diagram of the BusySynchronizedQueue unit test : BusySynchronized QueueTest main() The main thread coordinates 9 the other threads in the test

Partial Solution Using Java Synchronized Methods UML sequence diagram of the BusySynchronizedQueue unit test : BusySynchronized QueueTest consumer : Thread producer : Thread busyqueue : Busy SynchronizedQueue main() new() new() new() start() run() start() run() offer( ") poll() The consumer & producer threads generate & process messages sent via the BusySynchronizedQueue, 10 respectively

Partial Solution Using Java Synchronized Methods UML sequence diagram of the BusySynchronizedQueue unit test : BusySynchronized QueueTest consumer : Thread producer : Thread busyqueue : Busy SynchronizedQueue main() new() new() new() start() run() start() run() offer( ") poll() The offer() & poll() methods are synchronized, so the test runs correctly, but is inefficient 11 due to busy waiting!!

Implementation of the BusySynchronizedQueue 12

Implementation of the BusySynchronizedQueue Java synchronized methods protects critical sections from concurrent access class BusySynchronizedQueue<E> implements BoundedQueue<E> { private ListedList<E> mlist; private int mcapacity; public BusySynchronizedQueue(int capacity) { mcapacity = capacity; mlist = new LinkedList<>(); public synchronized boolean offer(e e) { if (!isfull()) { mlist.add(e); return true; else return false; public E synchronized poll() { return mlist.poll(); See github.com/douglascraigschmidt/posa/tree/ master/ex/m3/queues/busysynchronizedqueue 13

Implementation of the BusySynchronizedQueue Java synchronized methods protects critical sections from concurrent access class BusySynchronizedQueue<E> implements BoundedQueue<E> { private ListedList<E> mlist; private int mcapacity; public BusySynchronizedQueue(int capacity) { mcapacity = capacity; mlist = new LinkedList<>(); public synchronized boolean offer(e e) { if (!isfull()) { mlist.add(e); return true; else return false; Only one synchronized method can be active in any given object public E synchronized poll() { return mlist.poll(); 14

Implementation of the BusySynchronizedQueue Java synchronized methods protects critical sections from concurrent access class BusySynchronizedQueue<E> implements BoundedQueue<E> { private ListedList<E> mlist; private int mcapacity; public BusySynchronizedQueue(int capacity) { mcapacity = capacity; mlist = new LinkedList<>(); public synchronized boolean offer(e e) { if (!isfull()) { mlist.add(e); return true; else return false; Only one synchronized method can be active in any given object public E synchronized poll() { return mlist.poll(); This constraint may actually be a liability for certain types of concurrently accessed objects, e.g., double-ended 15 queues implemented as linked lists

Implementation of the BusySynchronizedQueue Java synchronized methods protects critical sections from concurrent access Adding the synchronized keyword has two effects class BusySynchronizedQueue<E> implements BoundedQueue<E> { private ListedList<E> mlist; private int mcapacity; public BusySynchronizedQueue(int capacity) { mcapacity = capacity; mlist = new LinkedList<>(); public synchronized boolean offer(e e) { if (!isfull()) { mlist.add(e); return true; else return false; public E synchronized poll() { return mlist.poll(); See docs.oracle.com/javase/tutorial/ 16 essential/concurrency/syncmeth.html

Implementation of the BusySynchronizedQueue Java synchronized methods protects critical sections from concurrent access Adding the synchronized keyword has two effects class BusySynchronizedQueue<E> implements BoundedQueue<E> { private ListedList<E> mlist; private int mcapacity; public BusySynchronizedQueue(int capacity) { mcapacity = capacity; mlist = new LinkedList<>(); public synchronized boolean offer(e e) { if (!isfull()) { mlist.add(e); return true; else return false; Invocations of offer() & poll() on the same object can t interleave public E synchronized poll() { return mlist.poll(); i.e., each synchronized 17 method is atomic?

Implementation of the BusySynchronizedQueue Java synchronized methods protects critical sections from concurrent access Adding the synchronized keyword has two effects class BusySynchronizedQueue<E> implements BoundedQueue<E> { private ListedList<E> mlist; private int mcapacity; public BusySynchronizedQueue(int capacity) { mcapacity = capacity; mlist = new LinkedList<>(); public synchronized boolean offer(e e) { Establishes a happensbefore relation to if (!isfull()) { mlist.add(e); return true; else ensure visibility of state return false; changes to all threads public E synchronized poll() { return mlist.poll(); See en.wikipedia.org/ 18 wiki/happened-before

Evaluating the Busy SynchronizedQueue 19

Evaluating the BusySynchronizedQueue There are limitations with Java synchronized methods when they are used alone See github.com/douglascraigschmidt/posa/tree/ master/ex/m3/queues/busysynchronizedqueue 20

There are limitations with Java synchronized methods when they are used alone class BusySynchronizedQueue<E> implements BoundedQueue<E> { private ListedList<E> mlist; private int mcapacity; public BusySynchronizedQueue(int capacity) { mcapacity = capacity; mlist = new LinkedList<>(); public synchronized boolean offer(e e) { if (!isfull()) { mlist.add(e); return true; Evaluating the BusySynchronizedQueue else return false; Concurrent calls to these methods will busy wait.. public E synchronized poll() { return mlist.poll(); See en.wikipedia.org/ wiki/busy_waiting 21

Evaluating the BusySynchronizedQueue There are limitations with Java synchronized methods when they are used alone class BusySynchronizedQueue<E> implements BoundedQueue<E> { private ListedList<E> mlist; private int mcapacity; public BusySynchronizedQueue(int capacity) { mcapacity = capacity; mlist = new LinkedList<>(); public synchronized boolean offer(e e) { if (!isfull()) { mlist.add(e); return true; else return false; Need to coordinate offer() & poll() so they won t busy wait when there s nothing to do public E synchronized poll() { return mlist.poll(); Java built-in monitor objects therefore provide wait & notify mechanisms 22 to avoid busy waiting

End of Java Monitor Objects: Synchronization (Part 3) 23