Paradigmas de Computação Paralela

Similar documents
Threads Questions Important Questions

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

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

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

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

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

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

CMSC 330: Organization of Programming Languages

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

Daemon thread, 7 Deadlock, 27

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

CMSC 132: Object-Oriented Programming II. Threads in Java

CMSC 330: Organization of Programming Languages

COMP 322: Fundamentals of Parallel Programming

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1)

Concurrent Programming using Threads

Phaser volle Energie...

Chapter 4: Multithreaded

Questions from last time

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

Java Threads. COMP 585 Noteset #2 1

COMP 322: Fundamentals of Parallel Programming. Lecture 30: Java Synchronizers, Dining Philosophers Problem

Chapter 32 Multithreading and Parallel Programming

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

Advanced Programming Concurrency

SUMMARY FUTURES CALLABLES CONCURRENT PROGRAMMING THREAD S ADVANCED CONCEPTS

CS11 Java. Fall Lecture 7

UNIT V CONCURRENT PROGRAMMING

CONCURRENT API AND PARALLEL PROGRAMMING

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

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

User Space Multithreading. Computer Science, University of Warwick

Threading and Synchronization. Fahd Albinali

Programming in Parallel COMP755

7. MULTITHREDED PROGRAMMING

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer.

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

Part I: Communication and Networking

Concurrency Utilities: JSR-166

Performance Throughput Utilization of system resources

Concurrent Programming

Note: Each loop has 5 iterations in the ThreeLoopTest program.

Java Threads. Introduction to Java Threads

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

Multi-threading in Java. Jeff HUANG

Chapter 4: Multi-Threaded Programming

Part IV Other Systems: I Java Threads

Info 408 Distributed Applications Programming Exercise sheet nb. 4

Figure 6.1 System layers

CMSC 330: Organization of Programming Languages. Concurrency & Multiprocessing

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

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

User-level threads. with threads. Paul Turner

Synchronization SPL/2010 SPL/20 1

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

Lecture 24: Java Threads,Java synchronized statement

The Java FutureTask. Douglas C. Schmidt

Threads and Parallelism in Java

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

Concurrency, Thread. Dongkun Shin, SKKU

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

COMP 322: Fundamentals of Parallel Programming

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Threads and Java Memory Model

Parallel Programming Languages COMP360

What's wrong with Semaphores?

Parallel Task Executor in Java

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set?

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

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

Threads and Locks, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014

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

-Aditya Bhave CSCI-5448 Graduate Presentation

Java Concurrency. Towards a better life By - -

Parallelism Marco Serafini

Principles of Software Construction: Concurrency, Part 2

MVP1: Introduction to concurrency in JAVA

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

JAVA CONCURRENCY FRAMEWORK. Kaushik Kanetkar

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

CS 159: Parallel Processing

Lecture 7: Process & Thread Introduction

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

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

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

COMP346 Winter Tutorial 4 Synchronization Semaphores

Concurrent Programming Benoît Garbinato

Chapter 4: Threads. Chapter 4: Threads

Parallel Programming Practice

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

Concurrency: a crash course

Le L c e t c ur u e e 7 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Multithreading

Parallel Programming Practice

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

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

F. Tip and M. Weintraub CONCURRENCY

Transcription:

Paradigmas de Computação Paralela Concurrent/Parallel Programming in OO /Java João Luís Ferreira Sobral jls@...

Specification of concurrency/parallelism Benefits from concurrent programming Programs that require multiple activities Active objects in real world Better service availability Supports asynchronous message/invocation Take advantage of parallelism on multi-core / multi-cpu systems Required concurrency (certain Java classes execute concurrently, ex. Swing, applet, beans) Concurrent/parallel activities: concepts Tasks versus Thread versus Process Parallelism: logic versus physical Pre-emption Scheduling and priorities

Specification of concurrency/parallelism Processes Used for unrelated tasks (e.g., a program) Own address space Address space is proteded from other process Swithching at the kernel level Threads Are part from the same job Share address space, code, data and files Swithching at the user or kernel level Every process has at lest one thread

Thread vs Process Code, data, files

Process/Thread scheduling user-level thread scheduling kernel-level thread scheduling

Process/Thread vs Tasks Task: sequence of instructions Thread/process: execution context for a task Processor/core: hardware that runs a thread/process In Java Runnable object Thread Processor core Threads are scheduled on available cores

Logic vs physical parallelism Intel SMT Pre-emption

Advantages/disadvantages of threads Benefits from threads Shared variables! Easy communications between tasks/contexts Multiple threads coordinate their execution and share data through reading and writing shared variables Problems Hidden dependencies are hard to debug Shared variables may be updated by other threads Performance prediction OOP to the rescue Object encapsulation to support threading Classes control access to shared data via synchronization

Specification of concurrency/parallelism Problems introduced by concurrent programming safety - inconsistencies in the execution of programs liveness - deadlocks in the execution of programs introduces non-determinism in program execution in OO systems there are fewer objects than asynchronous activities not useful for local execution of methods in a model of call / response introduces overhead due to the creation, scheduling and synchronization of threads Concurrency in traditional approaches Models based on fork/join, cobegin/coend, and parfor Synchronization is done using semaphores, barriers or monitors Active process (CSP) Makes processing through an active body, interacting through message passing: blocking synchronous, synchronous non-blocking or asynchronous

Specification of concurrency/parallelism Concurrency in object oriented applications Synchronous invocations (traditional models) The client is blocked while the method is executed by the server, even if there is no return value Asynchronous invocations with no return value (one way) When the invoked method does not return a value the client can continue running simultaneously with the execution of the method on the server. Asynchronous invocations with return value When there is a return value, the invocation can also be asynchronous There are three alternatives to get the return result: Synchronous deferred - The client makes a second invocation of the server to obtain the result Client DoTask() Server time GetResult()

Specification of concurrency/parallelism Concurrency in object oriented applications (cont.) Asynchronous invocations with return value (cont) With callback - The server performs an invocation of a predefined method of the client when the task completes Client DoTask() Result() Server With future - The invocation is delegated to another object that stores the result Client DoTask() Future DoTask() Server GetResult()

Concurrent Programming in Java Java was one of the first languages with support for concurrent programming Interface Runnable Must be implemented by classes to be executed by a thread Method run() contains the code to be executed interface Runnable { public void run(); Class java.lang.thread: (also implements the Runnable interface) Thread() or Thread(Runnable r); // class constructor start(); // creates a thread and invokes r.run() join(); // waits for thread completion sleep(int ms); // suspends the thread setpriority(int Priority); // changes thread priority

Concurrent Programming in Java Example (simpler option) Two threads increment their own counter public class Cont extends Thread { // implicit: implements Runnable public Cont() { public void run() { for (int i=0; i<100; i++) System.out.println(Thread.currentThread() + i= + i); Sequential execution:... new Cont().run(); new Cont().run();... - Parallel execution (fork&join model):... Thread t1 = new Cont(); Thread t2 = new Cont(); t1.start(); // fork t2.start(); // fork // or t2.run(); t1.join(); // wait for the end of t1 execution

Concurrent Programming in Java Example (more flexible alternative) Two threads increment their own counter public class Cont implements Runnable { public Cont() { public void run() { for (int i=0; i<100; i++) System.out.println( i= + i); Sequential execution:... new Cont().run(); new Cont().run();... // join - Parallel execution:... Cont c1 = new Cont(); Cont c2 = new Cont(); Thread t1 = new Thread(c1); Thread t2 = new Thread(c2); t1.start(); t2.start();... // t1.join, to wait for the end of execution

Concurrent Programming (in Java) Security - nothing bad should happen in a program Liveness - something good must happen in a program Example of lack of security: Execution of method inc() by two threads simultaneously can lead to a inconsistent value of variable ct public class Cont { protected long ct; public Cont() { ct=0; public void inc() { ct = ct + 1; Thread 1 ct = ct +1 Load $R1,ct Inc $R1 Store $R1,ct ct? Thread 2 ct = ct +1 Load $R2,ct Inc $R2 Store $R2,ct

Concurrent Programming (in Java) Specification of synchronization (increases security) Blocks of code and synchronized methods (mutex) synchronized method() {... / / method has exclusive access to the object synchronized(oneobj) {... / / Gets exclusive access to oneobj Java memory model A thread of execution can keep local copies of values. Synchronized blocks ensure that all threads "see" consistent values With monitors (implemented by the Object class) wait () - wait for access to the monitor wait (int timeout) - wait, with timing notify () - wakes up a thread waiting for access notifyall () - wakes up all threads waiting

Concurrent Programming (in Java) Example of a lack of liveness (deadlock): Execution of method inc () with two threads simultaneously on objects with cross-references Obj1/Thread1:... synchronized void inc() { obj2.inc() Obj2/Thread2:... synchronized void inc() { obj1.inc()

Concurrent Programming (in Java) Patterns to improve safety Stateless or immutable objects (e.g. String class) public int[] sort(int[] arr) { int[] copy = arr.clone(); // local copy // sort return(copy); Objects enclosed in other objects Patterns to improve liveness Methods that only read the object state usually do not need be synchronized (except double and long) No need to synchronize the variables that are written only once: void setend() { end = True;

Concurrent Programming (in Java) Patterns to improve liveness (cont.) Separated synchronization to access to parts of the state (or divide the state into two objects) Class twopoints { Point p1, p2; public void movexp1(int x) { synchronized (p1) { p1.movex(x); public void movexp2(int x) { synchronized (p2) { p2.movex(x); Resources should be accessed by the same order public void update() { synchronized(obj1) { synchronized(obj2) {... // do update

Asynchronous method invocation in Java With no return value Implemented through the pattern command, where the command is executed in parallel with the client. The command parameters are passed in the constructor Example: Writing data to file in background - activated by the client: public class FileWriter extends Thread { private String nm; private byte[] d; public FileWriter(String n, byte data[]) { nm = n; d = data; public void run() { writebytes(nm,d); // client code (new FileWriter( Pic,rawPicture)).start();

Asynchronous method invocation in Java Synchronous deferred Using the method Thread.join() r = new Service().start();.. // dowork(); r.join(); Client start() Service time r.getresult(); getresult() Future The future will contain the result of the operation and blocks the client if the vaue is requested it is available class Future extends Thread { private Task tk=null; public Future(Task tsk) { tk = tsk; start(); public Task getresult() { join(); return(tk); Client public void run() { tk = dotask(); // do task Future(t) getresult() Future dotask() Server // client code Future f = new Future(task); // do other work f.getresult();

Asynchronous method invocation in Java Callback public interface Client { public void opok(task); Client OPCallBack() Server opok() class OPCallBack extends Thread { private Client cl=null; private Task tk=null; public OPCallBack(Task tsk, Client clk) { tk = tsk; cl = clk start(); public run() { tk = dotask(tk); cl.opok(tk); // callback

Extensions in Java 5 Executors (Thread Pool) void Executor.execute(Runnable task) // Thread Pool interface Future<V> { V get(); // (new Thread(r)).start(); becomes e.execute(r) Future<T> Executor.submit(Callable<T> task) interface Callable<V> { V call(); High performance locks: (ReentrantLock, Condition, ReadWriteLock) Lock l =...; l.lock(); try { // access the resource protected by this lock finally { l.unlock(); interface Lock { lock(); trylock(); unlock(); Generic classes for synchronization: semaphores, mutexes, barriers, latches, and exchangers Concurrent collections: ConcurrentHashMap, BlockingQueue Atomic variables: java.util.concurrent.atomic

Extensions in Java 7/8 Lambda expressions can replace Runnable and Callable interfaces Avoids the overhead of creating a class and of passing parameters and returning a value Syntax: p1 [, p2, p3 ] -> { body statement Example: Person -> { Person.getAge() > 18; Steams use lambda functions to express parallel operations on collections int sum = widgets.parallelstream().filter(b -> b.getcolor() == RED).mapToInt(b -> b.getweight()).sum(); New executor: forkjoinpool (Java 7) Call from non-fork/join clients Call from within fork/join computations Arrange async execution execute(forkjointask) ForkJoinTask.fork() Await and obtain result invoke(forkjointask) ForkJoinTask.invoke() Arrange exec and obtain Future submit(forkjointask) ForkJoinTask.fork() (ForkJoinTasks are Futures)