A brief intro to: Parallelism, Threads, and Concurrency

Slides:



Advertisements
Similar presentations
Practical Session 6 Multitasking vs. multithreading Threads Concurrency vs. Parallelism Java Threads Thread confinement Object/Class Immutability.
Advertisements

3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
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.
Chap 4 Multithreaded Programming. Thread A thread is a basic unit of CPU utilization It comprises a thread ID, a program counter, a register set and a.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 2110: Parallel Programming A brief intro to: Parallelism, Threads, and Concurrency.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
Austin Java Users Group developerWorks article – µActor Library BARRY FEIGENBAUM, PH. D. 02/26/13.
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.
MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
1 Web Based Programming Section 8 James King 12 August 2003.
1 Threads  Sequential Execution: Here statements are executed one after the other.They consider only a single thread of execution, where thread is an.
Concurrent Programming and Threads Threads Blocking a User Interface.
Synchronizing threads, thread pools, etc.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Java Thread and Memory Model
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
CMSC 330: Organization of Programming Languages Threads.
1 OS Review Processes and Threads Chi Zhang
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Multithreaded Programming in Java David Meredith Aalborg University.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
Java Thread Programming
Multithreading The objectives of this chapter are:
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
OPERATING SYSTEM CONCEPT AND PRACTISE
Multi Threading.
Multithreaded Programming in Java
CS399 New Beginnings Jonathan Walpole.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Atomicity CS 2110 – Fall 2017.
Multithreading Chapter 23.
Synchronization Lecture 23 – Fall 2017.
Thread Implementation Issues
Dr. Mustafa Cem Kasapbaşı
Multithreaded Programming
Unit 1 Lab14 & Lab15.
Processes in Unix, Linux, and Windows
Threads in Java James Brucker.
CS510 Operating System Foundations
Representation and Management of Data on the Internet
Chapter 4: Threads.
Multithreaded Programming in Java
Multithreading The objectives of this chapter are:
CMSC 202 Threads.
Presentation transcript:

A brief intro to: Parallelism, Threads, and Concurrency

Goals Appreciate the (increasing) importance of parallel programming Understand fundamental concepts: Parallelism, threads, multi-threading, concurrency, locks, etc. See some basics of this is done in Java

Notes! An area of rapid change! Variations between languages 1990s: parallel computers were $$$$ Now: 4 core machines are commodity Variations between languages Evolving frameworks, models, etc.

(Multi)Process vs (Multi)Thread Assume a computer has one CPU Can only execute one statement at a time Thus one program at a time Process: an operating-system level “unit of execution” Multi-processing Op. Sys. “time-slices” between processes Computer appears to do more than one program (or background process) at a time

Tasks and Threads Thread: “a thread of execution” “Smaller”, “lighter” than a process smallest unit of processing that can be scheduled by an operating system Has its own run-time call stack, copies of the CPU’s registers, its own program counter, etc. Process has its own memory address space, but threads share one address space A single program can be multi-threaded Time-slicing done just like in multiprocessing Repeat: the threads share the same memory

Thread Properties In Java ID: This attribute stores a unique identifier for each Thread. Name: This attribute store the name of Thread. Priority: This attribute stores the priority of the Thread objects. Threads can have a priority between one and 10, where one is the lowest priority and 10 is the highest one. It's not recommended to change the priority of the threads, but it's a possibility that you can use if you want. Status: This attribute stores the status of Thread. In Java, Thread can be in one of these six states: new, runnable, blocked, waiting, time waiting, or terminated.

Task A task is an abstraction of a series of steps Might be done in a separate thread In Java, there are a number of classes / interfaces that basically correspond to this Example (details soon): Runnable work done by method run()

Library Classes for Concurrency For task-like things: Runnable, Callable Thread class Managing tasks and threads Executor, ExecutorService

Java’s Thread Class Class Thread: it’s method run() does its business when that thread is run But you never call run(). Instead, you call start() which lets Java start it and call run() To use Thread class directly (not recommended now): define a subclass of Thread and override run() – not recommended! Create a task as a Runnable, link it with a Thread, and then call start() on the Thread. The Thread will run the Runnable’s run() method.

Creating a Task and Thread Again, the first of the two “old” ways Get a thread object, then call start() on that object Makes it available to be run When it’s time to run it, Thread’s run() is called So, create a thread using inheritance Write class that extends Thread, e.g. MyThread Define your own run() Create a MyThread object and call start() on it We usually do not do this! Not good design!

Runnables and Thread Use the “task abstraction” and create a class that implements Runnable interface Define the run() method to do the work you want Now, two ways to make your task run in a separate thread First way: Create a Thread object and pass a Runnable to the constructor As before, call start() on the Thread object Second way: hand your Runnable to a “thread manager” object Several options here! These are the new good ways. More soon.

Nice to Have a Thread “Manager” If your code is responsible for creating a bunch of tasks, linking them with Threads, and starting them all, then you have muchto worry about: What if you start too many threads? Can you manage the number of running threads? Enough processors? Can you shutdown all the threads? If one fails, can you restart it?

Executors An Executor is an object that manages running tasks Submit a Runnable to be run with Executor’s execute() method So, instead of creating a Thread for your Runnable and calling start() on that, do this: Get an Executor object, say called exec Create a Runnable, say called myTask Submit for running: exec.execute(myTask)

How to Get an Executor Use static methods in Executors library. Fixed “thread pool”: at most N threads running at one time Executor exec = Executors.newFixedThreadPool(MAX_THREADS); Unlimited number of threads Executor exec = Executors.newCachedThreadPool();

Possible Bugs in Multithreaded Code Possible bug #1 i=1; x=10; x = i + x; // x could be 12 here Possible bug #2 if ( ! myList.contains(x) ) myList.add(x); // x could be in list twice Why could these cause unexpected results?

Synchronization Understand the issue with concurrent access to shared data? Data could be a counter (int) or a data structure (e.g. a Map or List or Set) A race condition: Two threads will access something. They “compete” causing a problem A critical section: a block of code that can only be safely executed by one thread at a time A lock: an object that is “held” by one thread at a time, then “released”

Synchronized Methods Common situation: all the code in a method is a critical section I.e. only one thread at a time should execute that method E.g. a getter or setter or mutator, or something that changes shared state info (e.g. a Map of important data) Java makes it easy: add synchronized keyword to method signature. E.g. public synchronized void update(…) {

Some Java Solutions There are some synchronized collections Classes like AtomicInteger Stores an int Has methods to operate on it in a thread-safe manner int getAndAdd(int delta) instead of i=i+1

Other Topics Other Java Libraries like Concurrency in Swing Semaphores UncaughtExceptionHandler

Plan Learning by doing One or more example for each topic First, the problem. Then the solution.

References to Read Goetz, B. et al.: Java Concurrency in Practice, Addison-Wesley 2010   González, J.: Java 7 Concurrency Cookbook. Packt Publishing 2012. Hartley, S.: Concurrent Programming, The Java Programming Language, Oxford University Press Inc. 1998 Lea, D.: Concurrent Programming in Java, design Principles and Patterns Second Edition, Addison-Wesley 2000

References and Tutorials Java API documentation: https://docs.oracle.com/javase/8/docs/api/ Java Tutorials http://docs.oracle.com/javase/tutorial/essential/concurrency http://www.tutorialspoint.com/java/java_multithreading.htm http://tutorials.jenkov.com/java-concurrency/index.html