Prepared by Oussama Jebbar

Slides:



Advertisements
Similar presentations
Chapter 2 Processes and Threads
Advertisements

A simple example finding the maximum of a set S of n numbers.
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
Definitions Process – An executing program
Multithreading.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Threads. Sequential control Sequential programs: begin execute end At any time, there is a single point of execution Threads: also structured as begin.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
111 © 2002, Cisco Systems, Inc. All rights reserved.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
© David Kirk/NVIDIA and Wen-mei W. Hwu, ECE 498AL, University of Illinois, Urbana-Champaign 1 Basic Parallel Programming Concepts Computational.
Threading Eriq Muhammad Adams J
Class Opener:. Identifying Matrices Student Check:
Synchronizing threads, thread pools, etc.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Sistem Operasi © Sekolah Tinggi Teknik Surabaya 1.
Professor: Shu-Ching Chen TA: Samira Pouyanfar.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int.
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
DOUBLE INSTANCE LOCKING A concurrency pattern with Lock-Free read operations Pedro Ramalhete Andreia Correia November 2013.
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Multi-Threading in Java
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Distributed and Parallel Processing George Wells.
Chapter 4 – Thread Concepts
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
A brief intro to: Parallelism, Threads, and Concurrency
Threaded Programming in Python
PROCESS MANAGEMENT IN MACH
Background on the need for Synchronization
PA1 Discussion.
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Chapter 4 – Thread Concepts
Multithreading Tutorial
Chapter 4: Multithreaded Programming
COEN346 Tutorial Monitor Objects.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Multithreading Chapter 23.
Unit-2 Divide and Conquer
Multithreading Tutorial
Midterm review: closed book multiple choice chapters 1 to 9
Java Based Techhnology
Multithreading.
Threads Chapter 4.
CSE 451 Autumn 2003 Section 3 October 16.
Threaded Programming in Python
Multithreading Tutorial
Multithreading Tutorial
Assignment 6 Recitation
Multithreading in java.
Lecture 19 Threads CSE /6/2019.
CMSC 202 Threads.
Presentation transcript:

Prepared by Oussama Jebbar Threads COEN 346 Prepared by Oussama Jebbar

Outline General Context Threads Creation: Runnables Threads Creation: Callables Synchronization

Threads The smallest execution unit Shared Memory They can Challenges Execute a process (sometimes even periodically) Return values Challenges Communication (synchronous Vs asynchronous) Scheduling Control access to shared data

Threads Creation: Runnables JAVA

Threads Creation: Callables JAVA

Threads Creation Python import threading #define a class for your thread class myThread (threading.Thread): #constructor def __init__(self): #put constructor logic here #run method that contains the thread routine def run(self): #put the logic of your routine here # Create new threads thread1 = myThread() thread2 = myThread() # Start new Threads thread1.start() thread2.start()

Mutexes in C++ Used to control access to shared data To prevent data from being altered simultaneously To make sure all threads access the right data (variable holding the right value) Include <mutex> use methods such as: lock or try_lock to make sure only one thread accesses the block of code Use the method unlock to release it after the thread is finished

Mutex Example

Common Threads Control Utilities join(): a method used to make a thread wait for another one to finish (Rendez-vous) detach(): used in C++ to tell a thread that it does not have to wait for another one Some frameworks and programming languages include suspend/resume utilities

Exercise Fork-join model is a multithreaded implementation of a divide and conquer algorithm in which the sub-problems can be solved independently In this exercise we will have to implement the Strassen algorithm for matrix multiplication in a fork-join model Strassen algorithm for matrix multiplication Let A and B two square matrices, and C is their product If A and B are not 2n * 2n matrices, fill the missing rows and columns with zeros to make them so Partition A and B into equally sized blocks of size 2n-1 * 2n-1 Calculate the following matrices M1 = (A11 + A22)*(B11 + B22) M2 = (A21 + A22)*B11 M3 = A11*(B12-B22) M4 = A22*(B21-B11) M5 = (A11+A12)*B22 M6 = (A21-A11)*(B11+B12) M7 = (A12-A22)*(B21+B22) Calculate the four blocks that compose the product C as follows C11 = M1 + M4 – M5 + M7 C12 = M3 + M5 C21 = M2 + M4 C22 = M1 – M2 + M3 + M6 Adjust the size of the resulting matrix (in case zeros were added in the first step of the algorithm) Q1: Propose a fork-join model to implement this algorithm Q2: What is the number of threads that the fork-join model you proposed use to calculate the product of two matrices of size n? Q3: Knowing that the machines you have in the lab can only run up to ten threads concurrently, can you propose a solution to make your implementation work with inputs of a big size

Hint! Decoupling function/method invocation from its execution Allows for a better use of resources and reuse of threads (thread pooling) Active Object pattern