Principles of Software Development

Slides:



Advertisements
Similar presentations
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Advertisements

Concurrency 101 Shared state. Part 1: General Concepts 2.
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.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Race Conditions Critical Sections Deker’s Algorithm.
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.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
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.
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.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Threads CS 3250 Some of these slides contain material by Professor Chuck Allison.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
1 Web Based Programming Section 8 James King 12 August 2003.
Multi-Threaded Programming Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Internet Software Development Controlling Threads Paul J Krause.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
Synchronizing threads, thread pools, etc.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Multiprocessor Cache Consistency (or, what does volatile mean?) Andrew Whitaker CSE451.
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.
Multi-Threading in Java
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 OS Review Processes and Threads Chi Zhang
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Advanced Tools for Multi- Threads Programming Avshalom Elmalech Eliahu Khalastchi 2010.
Thread Pools CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
Producer/Consumer CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
CSCD 330 Network Programming
Multithreading / Concurrency
Asynchronous Programming
PA1 Discussion.
Object-Orientated Analysis, Design and Programming
Lecture 24 Concurrency 2 (D&D 23) Date.
Section 5.7 Concurrency, Interference, and Synchronization.
SE /11/2018 If you think the internet is not working in its current incarnation, you can’t change the system through think-pieces and F.C.C. regulations.
Multithreaded Programming in Java
Principles of Software Development
Multithreading.
Principles of Software Development
Multithreading.
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
CS203 Lecture 15.
9. Threads SE2811 Software Component Design
Computer Science 2 06A-Java Multithreading
Threads and Multithreading
CSCD 330 Network Programming
9. Threads SE2811 Software Component Design
Problems with Locks Andrew Whitaker CSE451.
Lecture 19 Threads CSE /6/2019.
CSE 332: Concurrency and Locks
CMSC 202 Threads.
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
9. Threads SE2811 Software Component Design
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Principles of Software Development Concurrency CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu

Outline Synchronization USC CSCI 201L

Motivation for Synchronization Thread synchronization is used to coordinate the execution of dependent threads Sometimes we want to have some control over when threads execute or what code they are able to simultaneously access Shared resources pose potential problems to multi-threaded code since we don’t typically have control over when a thread will be switched into and out of the CPU USC CSCI 201L

Synchronization Program Analogy Assume I brought a piggy bank to class (shared resource) 100 students in the class have to deposit a penny into the piggy bank (independent threads) How many pennies should be in the bank at the end? Deposit 1¢ USC CSCI 201L

Synchronization Example #1 1 public class AddAPenny implements Runnable { 2 private static PiggyBank piggy = new PiggyBank(); 3 4 public void run() { 5 piggy.deposit(1); 6 } 7 8 public static void main(String [] args) { 9 for (int i=0; i < 100; i++) { 10 Thread t = new Thread(new AddAPenny()); 11 t.start(); 12 } 13 System.out.println("Balance = " + piggy.getBalance()); 14 } 15 } 16 17 class PiggyBank { 18 private int balance = 0; 19 public int getBalance() { 20 return balance; 21 } 22 public void deposit(int amount) { 23 int newBalance = balance + amount; 24 Thread.yield(); 25 balance = newBalance; 26 } 27 } 4 Executions The values are not close to 100 because of how soon the println executes after the threads are started. Another reason these values are not close to 100 because the CPU switches the threads out of the CPU before they are able to set the balance variable on line 29 That means that threads are overwriting the other threads USC CSCI 201L

Synchronization Example #2 1 import java.util.concurrent.ExecutorService; 2 import java.util.concurrent.Executors; 3 4 public class AddAPenny implements Runnable { 5 private static PiggyBank piggy = new PiggyBank(); 6 7 public void run() { 8 piggy.deposit(1); 9 } 10 11 public static void main(String [] args) { 12 ExecutorService executor = Executors.newCachedThreadPool(); 13 for (int i=0; i < 100; i++) { 14 executor.execute(new AddAPenny()); 15 } 16 executor.shutdown(); 17 // wait until all tasks are finished 18 while(!executor.isTerminated()) { 19 Thread.yield(); 20 } 21 22 System.out.println("Balance = " + piggy.getBalance()); 23 } 24 } 25 26 class PiggyBank { 27 private int balance = 0; 28 public int getBalance() { 29 return balance; 30 } 31 public void deposit(int amount) { 32 int newBalance = balance + amount; 33 Thread.yield(); 34 balance = newBalance; 35 } 36 } 4 Executions The ExecutorService ensures that all of the threads will complete execution before printing the balance This is what while(!executor.isTerminated()) does These values are not close to 100 because the CPU switches the threads out of the CPU before they are able to set the balance variable on line 29 That means that threads are overwriting the other threads These values are lower than the values on the previous slide (slightly) because there is a little more overhead with the ExecutorService USC CSCI 201L

Synchronization Example #3 1 import java.util.concurrent.ExecutorService; 2 import java.util.concurrent.Executors; 3 4 public class AddAPenny implements Runnable { 5 private static PiggyBank piggy = new PiggyBank(); 6 7 public void run() { 8 piggy.deposit(1); 9 } 10 11 public static void main(String [] args) { 12 ExecutorService executor = Executors.newCachedThreadPool(); 13 for (int i=0; i < 100; i++) { 14 executor.execute(new AddAPenny()); 15 } 16 executor.shutdown(); 17 // wait until all tasks are finished 18 while(!executor.isTerminated()) { 19 Thread.yield(); 20 } 21 22 System.out.println("Balance = " + piggy.getBalance()); 23 } 24 } 25 26 class PiggyBank { 27 private int balance = 0; 28 public int getBalance() { 29 return balance; 30 } 31 public void deposit(int amount) { 32 balance += amount; 33 } 34 } 4 Executions These outputs are closer to 100 (expected answer) because there is much less code in the deposit method, so fewer chances of being context switched out of the CPU USC CSCI 201L

Synchronization Overview The problems in the previous examples are based on the use of a shared variable across multiple threads (the PiggyBank object piggy) The OS switches threads out of the CPU at times unknown to us This seems to be happening inside the deposit(int) method after newBalance is set but before it is assigned back to balance This causes the old value of balance to be used in subsequent threads When we reduce the deposit(int) method to a single statement, we get higher values for balance because there are fewer locations at which the OS can switch the thread out of the CPU before setting the value of balance USC CSCI 201L

Race Conditions A race condition is a state where more than one thread is accessing the same variable and overwriting the other thread’s updates This is a common problem in multi-threaded programming A class is thread-safe if an object of the class does not cause a race condition in the presence of multiple threads The AddAPenny class in the previous examples is not thread-safe USC CSCI 201L

Critical Section To avoid race conditions, it is necessary to prevent more than one thread from simultaneously entering a certain part of the program This part of the program is called a critical section or critical region In the previous examples, the critical section would be the deposit(int) method in the PiggyBank class If only one thread at a time was able to access the deposit(int) method, we would not have a race condition on the shared variable balance Note that balance is shared in the static instance of PiggyBank 22 public void deposit(int amount) { 23 int newBalance = balance + amount; 24 Thread.yield(); 25 balance = newBalance; 36 } USC CSCI 201L