Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.

Slides:



Advertisements
Similar presentations
Categories of I/O Devices
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
490dp Synchronous vs. Asynchronous Invocation Robert Grimm.
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Concurrency CS 510: Programming Languages David Walker.
©Brooks/Cole, 2003 Chapter 7 Operating Systems Dr. Barnawi.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Thread Pools. 2 What’s A Thread Pool? A programming technique which we will use. A collection of threads that are created once (e.g. when server starts).
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
Advanced Operating Systems CIS 720 Lecture 1. Instructor Dr. Gurdip Singh – 234 Nichols Hall –
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Proactor Pattern Venkita Subramonian & Christopher Gill
1 (Worker Queues) cs What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Concurrent Programming and Threads Threads Blocking a User Interface.
CS333 Intro to Operating Systems Jonathan Walpole.
Practical Session 12 Reactor Pattern. Disadvantages of Thread per Client It's wasteful – Creating a new Thread is relatively expensive. – Each thread.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Consider the program fragment below left. Assume that the program containing this fragment executes t1() and t2() on separate threads running on separate.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
1 OS Review Processes and Threads Chi Zhang
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
SPL/2010 Reactor Design Pattern 1. SPL/2010 Overview ● blocking sockets - impact on server scalability. ● non-blocking IO in Java - java.niopackage ●
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Java Thread Programming
Multithreading / Concurrency
Thread Pools (Worker Queues) cs
Multithreading Lec 23.
Process concept.
Advanced Operating Systems CIS 720
Chapter 3: Process Concept
Multi Threading.
Advanced Topics in Distributed and Reactive Programming
Thread Pools (Worker Queues) cs
CS399 New Beginnings Jonathan Walpole.
Other Important Synchronization Primitives
Lecture 7 Processes and Threads.
Advanced Topics in Distributed and Reactive Programming
COT 5611 Operating Systems Design Principles Spring 2014
Operating Systems.
Multithreaded Programming
Threads Chapter 4.
Java Concurrency 17-Jan-19.
Chapter 4: Threads & Concurrency
Chapter 4: Threads.
Why Threads Are A Bad Idea (for most purposes)
Java Concurrency.
NETWORK PROGRAMMING CNET 441
Java Concurrency.
Threads and Multithreading
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
Why Threads Are A Bad Idea (for most purposes)
Advanced Topics in Functional and Reactive Programming
Java Concurrency 29-May-19.
Why Threads Are A Bad Idea (for most purposes)
Chapter 13: I/O Systems.
CMSC 202 Threads.
The reactor design pattern
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Thread per client and Java NIO
Threads and concurrency / Safety
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis

Thread Based vs Event Driven Programming Threads Procedure-oriented systems Large number of processes Shared data Events Message-oriented systems Small number of processes No data sharing Thread costs 1MB of overhead on 64bit OS Actor costs 400Byte of overhead

Building Blocks of These Programming Models thread-based event-driven monitor event handler scheduling event loop exported functions event types accepted by event handler returning from a procedure dispatching a reply executing a blocking procedure call dispatching a message, awaiting a reply waiting on condition variables awaiting messages

Threads: Programming Complexity Developing correct concurrent code is extremely difficulty Sharing a single state is the cause of all troubles Programmers must synchronize and coordinate between threads Done by using locking primitives This increases the complexity Composition Problem: Given two different components that are thread-safe Their composition is not always thread-safe! This also increases the programming complexity Given two different components that are thread-safe, a composition of them is not thread-safe per se. For instance, placing circular dependencies between multi-threaded components unknowingly can introduce severe deadlocks.

Threads: Synchronization Problems Liveness problems: Erroneous locking introduces deadlocks or livelocks! Performance Issues: Too coarse locks Slow down concurrent code This leads to degraded sequential execution performance Too fine locks Increase the danger of deadlocks/livelocks This increases locking overhead Overhead is any combination of excess or indirect computation time, memory, bandwidth, or other resources that are required to perform a specific task. These operations are not part of the task itself – but they ensure its success.

Threads: Debugging Difficulty Preemptive scheduling causes nondeterminism Every execution of threaded code progresses differently The use of Arbiters causes nondeterminism A memory arbiter is a device used in a shared memory system to decide, for each memory cycle, which CPU will be allowed to access that shared memory. Multithreading is error-prone, and very difficult to debug! Preemptive scheduling and the use of arbiters The state explosion as a result from all possible interleavings of multiple threads renders a reasonable execution analysis of concurrent code virtually impossible!

Threads Coupling Inherit only if you want to override some behavior! 1 2 3 4 5 6 7 89 public class MyThread extends Thread { public MyThread() { super("MyThread"); } public void run() { //Code //Started with a "new MyThread().start()" call Thread implementation used to extend Thread class! Couples the Runnable with its own Thread. Inherit only if you want to override some behavior! This job is better suited for an interface implementation!

Thread Decoupling 1 2 3 4 5 6 public class MyRunnable implements Runnable { public void run() { //Code } //Started with a "new Thread(new MyRunnable()).start()" call Decouples Thread creation from Runnable implementation Allows “feeding” a single Thread multiple Runnables! What happens when the runnable finishes executing it’s code? Thread is closed What is I need to run a new runnable? New thread is created! What about communication between threads?

Executors: Allows Thread Reuse Resources are limited The number of threads running in parallel is always limited Due to CPU bottleneck, database connection limit, or memory limitations Thread pools improve resource utilization The overhead of creating a new thread is significant! Thread pool is a collection of threads that are created once at start. Executor Queue Number of tasks is normally larger than the number of running threads An executor has a queue containing these tasks

Handling Tasks Thread pool has a fixed number of threads Each task asks for a thread when starting and returns the thread to the pool after finishing. When there are no available threads in the pool the thread that initiates the task waits till the pool is not empty Problem? Not fair Tasks might use threads for a long time. Which leads to starvation This is a scheduling problem!

Pool Size A large pool can cause starvation Each thread consumes resources memory, management overhead, etc. A small pool can cause starvation Incoming tasks wait for a free thread Therefore, you have to tune the thread pool size: The number of expected tasks The required average processing time of the tasks

Ideal Pool Size The main goal: 𝑊𝑇 = 𝑒𝑠𝑡𝑖𝑚𝑎𝑡𝑒𝑑 𝑎𝑣𝑒𝑟𝑎𝑔𝑒 𝑤𝑎𝑖𝑡𝑖𝑛𝑔 𝑡𝑖𝑚𝑒 Processing should continue while waiting for slow operations such as I/O 𝑊𝑇 = 𝑒𝑠𝑡𝑖𝑚𝑎𝑡𝑒𝑑 𝑎𝑣𝑒𝑟𝑎𝑔𝑒 𝑤𝑎𝑖𝑡𝑖𝑛𝑔 𝑡𝑖𝑚𝑒 𝑆𝑇 = 𝑒𝑠𝑡𝑖𝑚𝑎𝑡𝑒𝑑 𝑎𝑣𝑒𝑟𝑎𝑔𝑒 𝑝𝑟𝑜𝑐𝑒𝑠𝑠𝑖𝑛𝑔 𝑡𝑖𝑚𝑒 𝑓𝑜𝑟 𝑎 𝑡𝑎𝑠𝑘 About 𝑊𝑇 𝑆𝑇 +1 threads will keep the processor fully utilized For example, if 𝑊𝑇 is 20𝑚𝑠 and 𝑆𝑇 is 5𝑚𝑠, we will need 5 threads to keep the processor busy.

Workers vs Event Queue Workers: Event Queue: Synchronous call Worker blocks, executes code Call returns Event Queue: Events occur, stored in queue Process next event, executes callback as call “return”

Event Loop – ties everything together!

Tools used in conjunction with event-driven programming First Class Function Support The ability to pass functions as arguments to other functions, The ability to return functions as the values from other functions The ability to assign function to variables or storing them in data structures Callback Functions They are passed to the function that acts asynchronously as a parameter. When the asynchronous operation will finish, the callback will be executed. Closures The ability for a first class function to access variables outside its scope when created. Asynchronous IO A form of input/output processing that permits other processing to continue before the transmission has finished.

Java 8: Lambda Expression & Closure 1 2 3 4 5 6 7 8 9 10 11 12 13 public Function<Integer, Integer> make_func() { int n = 0; return arg -> { System.out.print(n + " " + arg + ": "); arg += 1; return n + arg; }; } public void main() { Function<Integer, Integer> x = make_fun(); for(int i = 0; i < 5; i++) System.out.println(x.apply(i)); Function is generic Java8 interface to implement lambdas Closure: Accessing “n” from inside the lambda in line 4, 6 is possible for read only. You can’t modify “n”! If “n” is a reference, you can modify the object referenced by it, but can’t change “n” itself! This is Java8 limitation by design. Callback: “x” is generated function – callback is done on line 12 using apply() (Function interface)

Reactor Pattern Channels Selector ThreadPool Allows Non-blocking IO from and to sockets. Contains event queue: 3 types of events: ACCEPT, READ, WRITE Selector Used to extract events from Channels Implements event loop: Handles events in sequential manner Executes ACCEPT/READ/WRITE operations Delegates response generation to workers ThreadPool Contains workers (threads) Used to handle response generation Handles: Decoding, tokenization, generating response, encoding response

Reactor Pattern – Important Bits Callbacks are used to change event types the channel is interested in: When channel is first created: READ When responses are ready to be sent: READ/WRITE When there are no responses left to send to client: READ When “close connection” flag is received: WRITE Event loop: Main server loop is an event loop Loops over events generated in every active channel sequentially With every client connecting a new channel is especially made for the client And it joins the even loop. Selector: Used to extract events from each active channel Once a new channel is created it is registered with the selector

CODE TIME!!!