dit UPM Tema 3: Concurrencia /clásicos /java Análisis y diseño de software José A. Mañas

Similar documents
Concurrent Object Oriented Languages

Programming Languages

5 Classical IPC Problems

Anexos. Diseño y construcción de un puente grúa automatizado de precisión

CS 361 Concurrent programming Drexel University Spring 2000 Lecture 14. The dining philosophers problem

CSC501 Operating Systems Principles. Process Synchronization

CS 3430/5430 Homework 3 Solution Due: Saturday, Feb. 25 (11:59 PM)

Two Types of Semaphores

Process Synchronization

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

Concept of a process

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Computer Networks. Process Cooperation Multithreaded Programming in Java. Laszlo Böszörmenyi Computer Networks Processes - 1

CS 351 Design of Large Programs Threads and Concurrency

Contents. 6-1 Copyright (c) N. Afshartous

Performance Throughput Utilization of system resources

Default Route de la configuración en el EIGRP

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Multi-Threaded Programming Design CSCI 201 Principles of Software Development

Propedéutico de Programación

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

i219 Software Design Methodology 12. Case study 1 Dining philosopher problem Kazuhiro Ogata (JAIST) Outline of lecture

FORMAS DE IMPLEMENTAR LOS OYENTES. A).- El oyente en una clase independiente con el constructor con un argumento y usando el método getactioncommand.

Overview: Multicore Architectures

Only one thread can own a specific monitor

Part IV Other Systems: I Java Threads

CS193k, Stanford Handout #8. Threads 3

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

b) Use one of your methods to calculate the area of figure c.

The dining philosophers problem. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 13

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Multithreading Pearson Education, Inc. All rights reserved.

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Threads Chate Patanothai

Chapter 2 Processes and Threads

Introduction to Operating Systems

Principles of Software Construction: Concurrency, Part 2

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

CSE Traditional Operating Systems deal with typical system software designed to be:

Question1 (10 points) : Patron Aggregator

COMP 150-CCP Concurrent Programming. Lecture 12: Deadlock. Dr. Richard S. Hall

Concurrency: Deadlock and Starvation. Chapter 6

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

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Real-Time Operating Systems M. 5. Process Synchronization

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

Java Threads. Introduction to Java Threads

CS180 Review. Recitation Week 15

Threads, Concurrency, and Parallelism

COMP346 Winter Tutorial 4 Synchronization Semaphores

7.1 Processes and Threads

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Faculty of Computers & Information Computer Science Department

Multithreaded Programming

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

CS370 Operating Systems

Program #3 - Airport Simulation

3C03 Concurrency: Starvation and Deadlocks

Process Synchronization

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

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

Chapter 32 Multithreading and Parallel Programming

Component-Based Software Engineering

7. MULTITHREDED PROGRAMMING

Synchronization. Peter J. Denning CS471/CS571. Copyright 2001, by Peter Denning

Process Synchronization. studykorner.org

PROGRAMACIÓN ORIENTADA A OBJETOS

CHAPTER 6: PROCESS SYNCHRONIZATION

Chamilo LMS - Feature #6015 Improve procedure to add new friends in the social network

Monitors; Software Transactional Memory

CS360 Lecture 12 Multithreading

SFDV3006 Concurrent Programming

Lecture 10: Introduction to Semaphores

Sections 01 (11:30), 02 (16:00), 03 (8:30) Ashraf Aboulnaga & Borzoo Bonakdarpour

1) Discuss the mutual exclusion mechanism that you choose as implemented in the chosen language and the associated basic syntax

Process Synchronization

Propedéutico de Programación

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

Concurrent Programming TDA381/DIT390

CS 10: Problem solving via Object Oriented Programming Winter 2017

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

Lesson 6: Process Synchronization

THREADS. Laboratório - Java

Concurrency. Chapter 5

Readers/Writers Problem. Readers/Writers: Scenario 1. Readers/Writers Problem. Today: Synchronization for Readers/Writers Problem

Processes The Process Model. Chapter 2 Processes and Threads. Process Termination. Process States (1) Process Hierarchies

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

Parallel & Concurrent Programming

OCTOBEAM. LED Lighting Effect USER MANUAL / MANUAL DE USUARIO

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Java Threads. COMP 585 Noteset #2 1

Global variables and resources are shared between threads within the same process. Each thread has its own stack and program counter (see Fig 2-7).

Introduction to Java Threads

Concurrency EECS /36

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

Producer/Consumer CSCI 201 Principles of Software Development

Synchronization Principles II

Lecture 27: Safety and Liveness Properties, Java Synchronizers, Dining Philosophers Problem

Transcription:

Análisis y diseño de software dit UPM Tema 3: Concurrencia /clásicos /java José A. Mañas 11.2.2017

método 1. identifique el estado campos privados del monitor 2. identifique métodos de test and set métodos synchronized 3. programe las condiciones de espera while (condición(...)) wait(); programe las condiciones de notificación if (condición(...)) notifyall(); classic examples 2

ejemplos semaphore synchronized start wait for all producer consumer object pool readers writers smokers philosophers classic examples 3

semaphore se trata de implementar un semáforo usando el patrón monitor public class MySemaphore { private...; public synchronized void acquire(int n) {......... public synchronized void release(int n) {......... classic examples 4

semaphore se trata de implementar un semáforo usando el patrón monitor public class MySemaphore { private int permits = 0; public synchronized void acquire(int n) { while (permits < n) waiting(); permits -= n; public synchronized void release(int n) { permits += n; notifyall(); classic examples 5

semaphore: synchro start public class SynchStart { private static final int N_THREADS = 20; public static void main(string[] args) throws InterruptedException { Semaphore semaphore = new Semaphore(0); for (int i = 0; i < N_THREADS; i++) { Thread thread = new MyThread(semaphore); thread.start(); System.out.println("wait..."); Thread.sleep(5000); semaphore.release(n_threads); classic examples 6

semaphore: synchro start class MyThread extends Thread { private final Semaphore semaphore; MyThread(Semaphore semaphore) { this.semaphore = semaphore; public void run() { try { semaphore.acquire(1); System.out.println("run"); catch (InterruptedException ignored) { classic examples 7

semaphore: wait for all public class JoinAll { private static final int N_THREADS = 20; public static void main(string[] args) throws InterruptedException { Semaphore semaphore = new Semaphore(0); for (int i = 0; i < N_THREADS; i++) { Thread thread = new MyThread(semaphore); thread.start(); System.out.println("wait..."); semaphore.acquire(n_threads); System.out.println("... done"); classic examples 8

semaphore: wait for all class MyThread extends Thread { private final Semaphore semaphore; MyThread(Semaphore semaphore) { this.semaphore = semaphore; public void run() { System.out.println("run"); semaphore.release(1); classic examples 9

ejemplo: productor - consumidor uno o más threads producen datos a su ritmo uno o más threads consumen datos a su ritmo hay que sincronizarlos parar a los productores cuando hay demasiados datos espera a meter parar a los consumidores cuando no hay datos espera a sacar productor cola buffer consumidor classic examples 10

escenario productor productor productor productor cola buffer consumidor consumidor consumidor classic examples 11

Buffer versión 1 public class Buffer<E> { private final int max; private final List<E> queue; public Buffer1(int max) { this.max = max; queue = new ArrayList<>(max); public synchronized void put(e s) {......... public synchronized E take() {......... classic examples 12

Buffer versión 1 public class Buffer<E> { private final int max; private final List<E> queue; public Buffer1(int max) { this.max = max; queue = new ArrayList<>(max); public synchronized void put(e s) { while (queue.size() >= this.max) waiting(); queue.add(s); notifyall(); public synchronized E take() { while (queue.isempty()) waiting(); notifyall(); return queue.remove(0); classic examples 13

Buffer versión 2 public class Buffer<E> { private final List<E> queue; private final Semaphore haysitio; private final Semaphore haydatos; private final Semaphore mutex; public Buffer2(int size) { queue = new ArrayList<>(size); haysitio = new Semaphore(size); haydatos = new Semaphore(0); mutex = new Semaphore(1); public void put(e s) throws InterruptedException { haysitio.acquire(); mutex.acquire(); try { queue.add(s); finally { mutex.release(); haydatos.release(); public E take() throws InterruptedException { haydatos.acquire(); mutex.acquire(); try { return queue.remove(0); finally { mutex.release(); haysitio.release(); classic examples 14

errores (fragilidad) public void put(e s) throws InterruptedException { mutex.acquire(); haysitio.acquire(); try { queue.add(s); finally { mutex.release(); haydatos.release(); public E take() throws InterruptedException { mutex.acquire(); haydatos.acquire(); try { return queue.remove(0); finally { mutex.release(); haysitio.release(); classic examples 15

java.util.concurrent.blockingqueue public interface BlockingQueue<E>... { /** * Inserts the specified element into this queue, waiting if necessary * for space to become available. */ void put(e e) throws InterruptedException; /** * Retrieves and removes the head of this queue, waiting if necessary * until an element becomes available. * * @return the head of this queue * @throws InterruptedException if interrupted while waiting */ E take() throws InterruptedException; classic examples 16

object pool public abstract class Pool<E> { private final BlockingQueue<E> queue; public Pool(int size) { this.queue = new ArrayBlockingQueue<E>(size); for (int i = 0; i < size; i++) queue.add(create()); protected abstract E create(); public E get() throws InterruptedException { return queue.take(); public void put(e data) throws InterruptedException { queue.put(data); classic examples 17

readers-writers situations in which many threads try to access the same shared resource at one time. Some threads may read and some may write, with the constraint that no process may access the share for either reading or writing, while another process is in the act of writing to it. (In particular, it is allowed for two or more readers to access the share at the same time.) classic examples 18

readers-writers public class TestSuite { private static final int N_READERS = 10; private static final int N_WRITERS = 3; public static void main(string[] args) { RW_Monitor monitor = new RW_Monitor(); for (int a = 0; a < N_READERS; a++) { ReaderAgent agent = new ReaderAgent(a, monitor); agent.start(); for (int a = 0; a < N_WRITERS; a++) { WriterAgent agent = new WriterAgent(a, monitor); agent.start(); classic examples 19

readers-writers public class ReaderAgent extends Thread { private final int id; private final RW_Monitor monitor; public class WriterAgent extends Thread { private final int id; private final RW_Monitor monitor; public ReaderAgent(int id, RW_Monitor monitor) public WriterAgent(int { id, RW_Monitor monitor) { this.id = id; this.id = id; this.monitor = monitor; this.monitor = monitor; @Override public void run() { while (true) { System.out.println(id + " working"); monitor.openreading(); System.out.println(id + " reading"); nap(1000); monitor.closereading(); @Override public void run() { while (true) { System.out.println(id + " working"); monitor.openwriting(); System.out.println(id + " WRITING"); nap(1000); monitor.closewriting(); classic examples 20

readers-writers public class RW_Monitor { private int nreadersin = 0; private int nwritersin = 0; private int nwriterswaiting = 0; estado que debe ser protegido consistencia de los 3 campos puede haber varios lectores, sin ningún escritor puede haber un escritor, sin ningún lector podemos añadir condiciones como que si hay un escritor esperando, tiene prioridad los escritores entran por orden FIFO si los lectores llevan mucho esperando, toman la prioridad... classic examples 21

readers-writers synchronized void openreading() { while (nwritersin > 0 nwriterswaiting > 0) waiting(); nreadersin++; synchronized void closereading() { nreadersin--; notifyall(); synchronized void openwriting() { nwriterswaiting++; while (nreadersin > 0 nwritersin > 0) waiting(); nwriterswaiting--; nwritersin++; synchronized void closewriting() { nwritersin--; notifyall(); classic examples 22

smokers Assume a cigarette requires three ingredients to make and smoke: tobacco, paper, and matches. There are three smokers around a table, each of whom has an infinite supply of one of the three ingredients one smoker has an infinite supply of tobacco, another has paper, and the third has matches. There is also a non-smoking agent who enables the smokers to make their cigarettes by arbitrarily (nondeterministically) selecting two of the supplies to place on the table. The smoker who has the third supply should remove the two items from the table, using them (along with their own supply) to make a cigarette, which they smoke for a while. Once the smoker has finished his cigarette, the agent places two new random items on the table. This process continues forever. classic examples 23

smokers public class TestSuite { public static void main(string[] args) { SmokersMonitor monitor = new SmokersMonitor(); Smoker smoker1 = new Smoker(1, monitor, PAPER, MATCHES); Smoker smoker2 = new Smoker(2, monitor, TOBACCO, MATCHES); Smoker smoker3 = new Smoker(3, monitor, TOBACCO, PAPER); smoker1.start(); smoker2.start(); smoker3.start(); for (; ; ) { monitor.put(ingredient.random()); nap(1000); public enum Ingredient { TOBACCO, PAPER, MATCHES; public static Ingredient random() { Ingredient[] values = values(); int random = (int) (Math.random() * values.length); return values[random]; classic examples 24

smokers public class Smoker extends Thread { private final int id; private final SmokersMonitor monitor; private final Ingredient need1; private final Ingredient need2; public Smoker(int id, SmokersMonitor monitor, Ingredient need1, Ingredient need2) { this.id = id; this.need1 = need1; this.need2 = need2; this.monitor = monitor; @Override public void run() { while (true) { monitor.need(need1, need2); System.out.println(id + " smoking"); classic examples 25

smokers public class SmokersMonitor { private Set<Ingredient> available = new HashSet<>(); public synchronized void put(ingredient ingredient) { available.add(ingredient); notifyall(); public synchronized void need(ingredient need1, Ingredient need2) { while (!available.contains(need1)!available.contains(need2)) waiting(); available.remove(need1); available.remove(need2); classic examples 26

dining philosophers Cinco filósofos se sientan alrededor de una mesa y pasan su vida cenando y pensando. Cada filósofo tiene un plato de fideos y un tenedor a la izquierda de su plato. Para comer los fideos son necesarios dos tenedores y cada filósofo sólo puede tomar los que están a su izquierda y derecha. Si cualquier filósofo toma un tenedor y el otro está ocupado, se quedará esperando, con el tenedor en la mano, hasta que pueda tomar el otro tenedor, para luego empezar a comer. classic examples 27

dining philosophers public class TestSuite { public static void main(string[] args) { PhilosophersMonitor monitor = new PhilosophersMonitor(); for (Fork fork : Fork.values()) monitor.put(fork); Philosopher philo1 = new Philosopher(1, monitor, F1, F2); Philosopher philo2 = new Philosopher(2, monitor, F2, F3); Philosopher philo3 = new Philosopher(3, monitor, F3, F4); Philosopher philo4 = new Philosopher(4, monitor, F4, F5); Philosopher philo5 = new Philosopher(4, monitor, F5, F1); philo1.start(); philo2.start(); philo3.start(); philo4.start(); philo5.start(); public enum Fork { F1, F2, F3, F4, F5 classic examples 28

dining philosophers public class Philosopher extends Thread { private final int id; private final PhilosophersMonitor monitor; private final Fork need1; private final Fork need2; public Philosopher(int id, PhilosophersMonitor monitor, Fork need1, Fork need2) { this.id = id; this.need1 = need1; @Override this.need2 = need2; public void run() { this.monitor = monitor; while (true) { System.out.println(id + " thinking"); nap(1000); monitor.need(need1, need2); System.out.println(id + " eating"); nap(1000); monitor.put(need1); monitor.put(need2); classic examples 29

dining philosophers public class PhilosophersMonitor { private Set<Fork> available = new HashSet<>(); public synchronized void put(fork fork) { available.add(fork); notifyall(); public synchronized void need(fork need1, Fork need2) { while (!available.contains(need1)!available.contains(need2)) waiting(); available.remove(need1); available.remove(need2); classic examples 30