Multithreading. DCS – SWC 2 What is a thread Inside a single process, multiple threads can be executing concurrently A thread is the execution of (part.

Slides:



Advertisements
Similar presentations
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
1 Race Conditions When threads share access to a common object/data, they can conflict with each other and mess up the consistency of the object/data.
Locks (Java 1.5) Only one thread can hold a lock at once –Other threads that try to acquire it block (or become suspended) until lock becomes available.
Chapter 9 (Horstmann’s Book) Multithreading Hwajung Lee.
Multithreading. RHS – SWC 2 What is a thread Inside a single process, multiple threads can be executing concurrently A thread is the execution of (part.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Concurrency…leading up to writing a web crawler. Web crawlers.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
ThreadThread Thread Basics Thread Synchronization Animations.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
50.003: Elements of Software Construction Week 5 Basics of Threads.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Multithreading. Chapter Goals To understand how multiple threads can execute in parallel To learn how to implement threads To understand race conditions.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
1 CMSC 341: Data Structures Nilanjan Banerjee Data Structures University of Maryland Baltimore County
Next week (July 21) –No class –No office hour. Problem Multiple tasks for computer –Draw & display images on screen –Check keyboard & mouse input –Send.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Games Development 2 Concurrent Programming CO3301 Week 9.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
Internet Software Development Controlling Threads Paul J Krause.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
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.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Multi-Threading in Java
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Agenda  Quick Review  Finish Introduction  Java 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.
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.
CSCD 330 Network Programming
Multithreading / Concurrency
Multi Threading.
Background on the need for Synchronization
CSE 501N Fall ‘09 21: Introduction to Multithreading
Multithreading Chapter 9.
Lecture 21 Concurrency Introduction
ITEC324 Principle of CS III
Java Concurrency.
CS333 Intro to Operating Systems
Java Concurrency.
Threads and Multithreading
CSCD 330 Network Programming
Chapter 9 Multithreading
Presentation transcript:

Multithreading

DCS – SWC 2 What is a thread Inside a single process, multiple threads can be executing concurrently A thread is the execution of (part of) the program code, running independently of other threads Threads can share data, but local data can also belong to a specific thread

DCS – SWC 3

4

5 What is a thread Do threads really run in parallel? Depends on your system… At any time, there can be as many threads running as there are CPUs (actually CPU cores) in your system If more threads are running, the operating system will use time-slicing

DCS – SWC 6 What is a thread If only one CPU is available, and 10 threads are running, each thread is only truly running 10 % of the time OS manages threads using time slices –Threads are queued up –Each thread gets to run in a time-slice –It is then suspended, and put back in queue

DCS – SWC 7 What is a thread Why use threads at all…? It is sometimes unacceptable if a long operation blocks the application Think about an Internet browser; while one page is loading, you can still load other pages, scroll a page, etc. Using threads does not as such reduce the computation effort

DCS – SWC 8 Using threads Simple use of threads follows a few steps: First, implement a class which implements the Runnable interface: public interface Runnable { void run(); }

DCS – SWC 9 Using threads Next, put the code which needs to run in a a separate thread into the run method: public class MyTask implements Runnable { public void run() { // Code for task here... }

DCS – SWC 10 Using threads When the code is to be executed, create an object of your class, and a Thread object taking your object as input Finally, start the thread Runnable r = new MyTask(…); Thread t = new Thread(r); t.start();

DCS – SWC 11 Using threads Simple use of threads is thus not in itself particularly complicated When the started task ends, the thread itself is terminated What if we need to stop a thread, before the task has ended (or task is ”infinite”)…?

DCS – SWC 12 Using threads public void run() { while (true) { if (theQueue.isEmpty()) sleep(100); else processElement(theQueue.pop()); }

DCS – SWC 13 Using threads We cannot directly stop a thread, but we can ask it to stop itself… Call the method interrupt on the thread object The thread itself can check for interruption by calling Thread.interrupted

DCS – SWC 14 Using threads public void run() { while (!Thread.interrupted()) { if (theQueue.isEmpty()) sleep(100); else processElement(theQueue.pop()); }

DCS – SWC 15 Using threads But…what if the thread is sleeping? If interrupt is called on a sleeping thread, an InterruptedException is thrown in the thread We must catch this exception in run

DCS – SWC 16 Using threads public void run() { try { while (!Thread.interrupted()) { if (theQueue.isEmpty()) sleep(100); else processElement(theQueue.pop()); } catch (InterruptedExeption ex) {...} // Code for cleaning up, if needed }

DCS – SWC 17 Using threads Can a thread refuse to terminate…? Yes, but why should it… Calling interrupt on a thread is a signal to –Stop what you are doing –Clean up after yourself –Terminate completely We assume that threads are always willing to cooperate

Using threads Threads can also be used to perform a (computationally heavy) task in parallel Only makes sense if the task can be split into independent subtasks –Searching through large data –Image rendering –… DCS – SWC 18

Using threads Typical setup –Divide task into suitable subtasks –Devise way for subtask to report its result –Run each subtask in separate threads –Main thread will have to wait for all ”worker threads” to complete DCS – SWC 19

Using threads How can threads ”wait for each other”? If thread A wishes to wait for thread B to complete, thread A should make to call: threadB.join(); Thread A is often the main thread DCS – SWC 20

Using threads Main thread waiting for worker threads: for (Thread t : workerThreads) { try { t.join(); } catch (Exception ex) { // Handle exception if needed… } DCS – SWC 21

DCS – SWC 22 Using threads DEMO ThreadDemo project

DCS – SWC 23 Collision prevention On a railway through a mountain area, it was at some point only possible to build a tunnel with room for one track (see drawing below). This poses a problem when a train wants to pass through the tunnel – how can it be sure that no train is coming from the opposite direction at the same time? Devise a way for making sure that only one train uses the tunnel at any time. This problem is from the pre-radio era, so no solution involving remote communication is legal Make sure to take note of any assumptions you make when designing your solution Tunnel

DCS – SWC 24 Race conditions As long as threads only access their own local data (data created in their own thread), things are pretty easy If threads try to access (and change) shared data, things get complicated So-called race conditions can occur

DCS – SWC 25 Race conditions public void deposit(int amount) { int newBalance = balance + amount; balance = newBalance; } public void withdraw(int amount) { int newBalance = balance - amount; balance = newBalance; } balance = 1000 // Called on sepa- // rate threads deposit(100); withdraw(100); newBalance(d) = 1100 newBalance(w) = 900 balance = 900; balance = 1100;

DCS – SWC 26 Race conditions A very nasty property of race conditions is that they only occur sometimes… Allocation of time slices to threads is not deterministic – depends on total state of the system Code running correctly on one system may run incorrectly on another system

DCS – SWC 27 Race conditions Maybe this will help: public void deposit(int amount) { balance = balance + amount; } Unfortunately not – interruption is at lower level, so statement is not ”atomic”

DCS – SWC 28 Syncronised object access We must somehow prevent threads from simultaneous access to shared objects This is done by using a Lock object The Lock type is actually an interface – most commonly used implementation is the ReentrantLock class

DCS – SWC 29 Syncronised object access public class BankAccount { private Lock balanceLock; private int balance; public BankAccount() { balance = 0; balanceLock = new ReentrantLock(); }

DCS – SWC 30 Syncronised object access public void deposit(int amount) { balanceLock.lock(); balance = balance + amount; balanceLock.unlock(); } public void withdraw(int amount) { balanceLock.lock(); balance = balance - amount; balanceLock.unlock(); } This is almost – but not quite – good enough…

DCS – SWC 31 Syncronised object access If a thread calls lock on the Lock object, the thread owns the lock, until it calls unlock on the Lock object Any other thread calling lock on the Lock object will be suspended, until the lock becomes available What if the thread holding the lock never calls unlock …?

DCS – SWC 32 Syncronised object access public void aMethod() { aLock.lock(); // Code accessing a shared // resource aLock.unlock(); } What if this code throws an exception…?

DCS – SWC 33 Syncronised object access We must be sure that the thread holding the lock will call unlock Otherwise, we could have a deadlock We can ensure the call by using a finally clause

DCS – SWC 34 Syncronised object access public void aMethod() { aLock.lock(); try { // Code accessing a shared // ressource } finally { aLock.unlock(); } ALWAYS executed!

DCS – SWC 35 Preventing deadlocks Using locks ensures that shared data remains consistent, even in a multi- threaded scenario However, using locks may lead to other problems, typically a deadlock A deadlock occurs when two threads wait for each other to release locks

DCS – SWC 36 Preventing deadlocks I have the lock, but I’m waiting for B… I cannot proceed before I get the lock…

DCS – SWC 37 Preventing deadlocks public void withdraw(int amount) { balanceLock.lock(); try { while (balance < amount) // Wait for balance to increase } finally { balanceLock.unlock(); } But now it is impossible to deposit money…

DCS – SWC 38 Preventing deadlocks One solution is to use a so-called condition object Enables a thread to release a lock temporarily, thereby allowing other threads to obtain it A condition object is always related to a lock object

DCS – SWC 39 Preventing deadlocks I’m waiting for a condition to be fulfilled… OK, I’ll tell you when it might be fulfilled

DCS – SWC 40 Preventing deadlocks public class BankAccount { private Lock balanceLock; private Condition sufficientFundsCondition; private int balance; public BankAccount() { balance = 0; balanceLock = new ReentrantLock(); sufficientFundsCondition = balanceLock.newCondition(); }

DCS – SWC 41 Preventing deadlocks public void withdraw(int amount) { balanceLock.lock(); try { while (balance < amount) sufficientFundsConditions.await(); } finally { balanceLock.unlock(); } Not sleeping, but waiting…

DCS – SWC 42 Preventing deadlocks public void deposit(int amount) { balanceLock.lock(); try {... sufficientFundsConditions.signalAll(); } finally { balanceLock.unlock(); } Tell threads waiting on this condition, that the condition might have changed

DCS – SWC 43 Preventing deadlocks Notice that this is a coordinated effort between deposit and withdraw We must have control over all pieces of code trying to access a shared object Common errors: –Calling await without calling signalAll –Calling signalAll without locking the lock

DCS – SWC 44 Multithreading – final remarks Working with multi- threading is difficult! Hard to get an overview of all possible scenarios and consequences Errors may only show up occasionally

DCS – SWC 45 Multithreading – final remarks Debugging a multi- threaded program can be a true nightmare… The debugging process itself may change the behavior of the program Timing changes when debugging

DCS – SWC 46 Multithreading – final remarks Use multithreading only if you really need to! Typical example: GUI application with lengthy operations Makes GUI responsive when lengthy operations are executing

DCS – SWC 47 Multithreading – final remarks Making a program multi- threaded does not make it faster as such Amount of computation does not become smaller Multi-core CPUs make things a bit more compli- cated…