Threads and Concurrency in Java: Part 2

Similar documents
Threads and Concurrency in Java: Part 1

Threads and Concurrency in Java: Part 1

Threads and Concurrency in Java

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames

Exceptions. Your computer takes exception. The Exception Class. Causes of Exceptions

Python Programming: An Introduction to Computer Science

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Implementing Consistency -- Paxos. Some slides from Michael Freedman

CS 11 C track: lecture 1

Chapter 4 The Datapath

COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. Chapter 4. The Processor. Part A Datapath Design

CMSC Computer Architecture Lecture 15: Multi-Core. Prof. Yanjing Li University of Chicago

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

n Some thoughts on software development n The idea of a calculator n Using a grammar n Expression evaluation n Program organization n Analysis

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000.

n Maurice Wilkes, 1949 n Organize software to minimize errors. n Eliminate most of the errors we made anyway.

TRANSACTION MANAGEMENT [CH 16]

CMPT 125 Assignment 2 Solutions

Chapter 10. Defining Classes. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

UNIVERSITY OF MORATUWA

Programming with Shared Memory PART II. HPC Spring 2017 Prof. Robert van Engelen

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

CS 111 Green: Program Design I Lecture 27: Speed (cont.); parting thoughts

Abstract. Chapter 4 Computation. Overview 8/13/18. Bjarne Stroustrup Note:

Customer Portal Quick Reference User Guide

Lecture 28: Data Link Layer

Weston Anniversary Fund

Python Programming: An Introduction to Computer Science

Computational Geometry

Lecture 5. Counting Sort / Radix Sort

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. Chapter 4. The Processor. Single-Cycle Disadvantages & Advantages

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Analysis of Algorithms

CS211 Fall 2003 Prelim 2 Solutions and Grading Guide

Announcements. Reading. Project #4 is on the web. Homework #1. Midterm #2. Chapter 4 ( ) Note policy about project #3 missing components

Chapter 3. More Flow of Control. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Morgan Kaufmann Publishers 26 February, COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. Chapter 5

CS 111: Program Design I Lecture # 7: First Loop, Web Crawler, Functions

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs

Chapter 5. Functions for All Subtasks. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Reliable Transmission. Spring 2018 CS 438 Staff - University of Illinois 1

Recursion. Recursion. Mathematical induction: example. Recursion. The sum of the first n odd numbers is n 2 : Informal proof: Principle:

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

MR-2010I %MktBSize Macro 989. %MktBSize Macro

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design

CMSC Computer Architecture Lecture 12: Virtual Memory. Prof. Yanjing Li University of Chicago

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs

Introduction to Wireless & Mobile Systems. Chapter 6. Multiple Radio Access Cengage Learning Engineering. All Rights Reserved.

. Written in factored form it is easy to see that the roots are 2, 2, i,

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis

Task scenarios Outline. Scenarios in Knowledge Extraction. Proposed Framework for Scenario to Design Diagram Transformation

Deadlocks Detection and Avoidance. Prof. Sirer CS 4410 Cornell University

Chapter 2: Processes & Threads. Chapter 2

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015

CMSC22200 Computer Architecture Lecture 9: Out-of-Order, SIMD, VLIW. Prof. Yanjing Li University of Chicago

Bike MS: 2013 Participant Center guide

Bike MS: 2014 Participant Center guide

Behavioral Modeling in Verilog

Message Integrity and Hash Functions. TELE3119: Week4

Examples and Applications of Binary Search

Lecture 9: Exam I Review

Overview. Chapter 18 Vectors and Arrays. Reminder. vector. Bjarne Stroustrup

Investigation Monitoring Inventory

Guide to Applying Online

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

CSE 417: Algorithms and Computational Complexity

CMSC Computer Architecture Lecture 11: More Caches. Prof. Yanjing Li University of Chicago

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Classes and Objects. Again: Distance between points within the first quadrant. José Valente de Oliveira 4-1

Analysis of Algorithms

CS 683: Advanced Design and Analysis of Algorithms

UNIVERSITY OF MORATUWA

MapReduce and Hadoop. Debapriyo Majumdar Data Mining Fall 2014 Indian Statistical Institute Kolkata. November 10, 2014

top() Applications of Stacks

Review: The ACID properties

Sharing Collections. Share a Collection via . Share a Collection via Google Classroom. Quick Reference Guide

Creating Test Harnesses and Starter Applications

Data diverse software fault tolerance techniques

CS 111: Program Design I Lecture 19: Networks, the Web, and getting text from the Web in Python

Introduction to Sigma Notation

Ones Assignment Method for Solving Traveling Salesman Problem

Data Structures and Algorithms. Analysis of Algorithms

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Data structures. Appetizer. Appetizer

6.854J / J Advanced Algorithms Fall 2008

System and Software Architecture Description (SSAD)

Computers and Scientific Thinking

CS 111: Program Design I Lecture 18: Web and getting text from it

COSC 1P03. Ch 7 Recursion. Introduction to Data Structures 8.1

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov

Java net programming II

Parabolic Path to a Best Best-Fit Line:

A Taste of Maya. Character Setup

ECE4050 Data Structures and Algorithms. Lecture 6: Searching

Homework 1 Solutions MA 522 Fall 2017

Transcription:

Threads ad Cocurrecy i Java: Part 2 1

Waitig Sychroized methods itroduce oe kid of coordiatio betwee threads. Sometimes we eed a thread to wait util a specific coditio has arise. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 2

Waitig by Pollig For example i a Couter class we might wat to wait util the cout is 0 or less. We could do this class Couter { private it cout ; Note that waitforzeroorless is ot sychroized public Couter( it cout ) { this.cout = cout ; sychroized void decremet() { cout -= 1; sychroized itgetcout() { retur cout ; void waitforzeroorless() { // Pollig loop while( getcout() > 0 ) /*do othig*/; 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 3

Waitig by Pollig Slightly improvig this we ca write void waitforzeroorless() { // Pollig loop while( getcout() > 0 ) Thread.yield(); which keeps the thread from hoggig the CPU while pollig. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 4

Waitig by wait ad otifyall Better, we ca have a thread wait util it is otified that the coditio has (or may have) come about. While waitig, the thread reliuishes owership ad waits util some later time. To wait, a thread seds the object a wait message. To allow other threads to stop waitig, threads seds a otifyall message to the object. wait ad otifyall are methods of class Object. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 5

The Room metaphor Whe a thread i the room seds a wait message, it leaves the room ad eters the waitig room. Later aother thread eters. Add a waitig room Waitig room The atechamber The T1 room 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 6 T2 It seds a otifyall message. All threads i the waitig room move to the atechamber. Ad so they ca reeter whe the room is empty.

wait ad otifyall example. class Couter { private it cout ; Note that waitforzeroorless is sychroized public Couter( it cout ) { this.cout = cout ; sychroized void decremet() { cout -= 1 ; if( cout <= 0 ) otifyall() ; sychroized void waitforzero() { while( cout > 0 ) { try { wait(); catch( IterruptedExceptio e ) { 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 7

A possible seuece Oe thread Calls waitforzero eters the room Sees cout is 1. calls wait, eters the waitig room waits i waitig room waits i atechamber reeters the room returs from wait sees cout is 0 ad returs Aother thread Calls decremet eters the room. cout-- call otifyal,l movig other thread to wait ueue for the lock returs leavig the room 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 8

Passig messages We wat to pass messages betwee ay umber of producers ad cosumers executig o ay umber of threads. producer producer producer cosumer cosumer sed(0) receive() :Mailbox 0 { 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 9

Passig messages We wat to pass messages betwee threads so that Each message is received o more tha oce No message is overwritte before it is received. A receiver may have to wait util a ew message is set A seder may have to wait util there is room i the ueue Up to 100 messages ca be set but ot yet received. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 10

Passig messages Receivers may eed to wait. Suppose the mailbox iitially has o messages producer producer producer cosumer cosumer sed(1) receive() :Mailbox 1 { 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 11

Passig messages Producers may eed to wait. Suppose the capacity is 5 ad the mailbox is full producer producer producer cosumer cosumer sed(6) receive() :Mailbox 1 { 2 3 4 56 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 12

Passig messages icomplete class Mailbox<MessageType> { private static fial it CAP = 100 ; private Queue<MessageType> = ew Queue<MessageType>() ; // ivariat:.size() <= CAP public sychroized void sed(messagetype mess) { // wait util.size()< CAP.put( mess ); To do public sychroized MessageType receive() { // wait util.size() > 0 retur.take(); To do 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 13

Passig messages add waits class Mailbox<MessageType> { private static fial it CAP = 100 ; private Queue<MessageType> = ew Queue<MessageType>() ; // ivariat:.size() <= CAP public sychroized void sed(messagetype mess) { // wait util.size()< CAP while(.put( mess.size()==cap ) ; ) { try { wait() ; catch( IterruptedExceptio e ) { public sychroized MessageType receive() { // wait util.size() > 0 while( retur.size()==0.take() ) { try { wait() ; catch( IterruptedExceptio e ) { To do To do 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 14

Passig messages add otificatios class Mailbox<MessageType> { private static fial it CAP = 100 ; private Queue<MessageType> = ew Queue<MessageType>() ; // ivariat:.size() <= CAP public sychroized void sed(messagetype mess) { // wait util.size()< CAP while(.size()==cap ) { try { wait() ; catch( IterruptedExceptio e ) {.put( mess ); otifyall() ; public sychroized MessageType receive() { // wait util.size() > 0 while(.size()==0 ) { try { wait() ; catch( IterruptedExceptio e ) { retur otifyall();.take(); Doe

Eve better tha wait ad otifyall As software gets more complex, usig wait ad otifyall ca be a bit awkward ad is easy to mess up. A improvemet is to use Dr. Norvell s moitor package. See http://www.egr.mu.ca/~theo/misc/moitors/moitors.html 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 16

Deadlock While waitig ca solve safety issues (e.g. esurig ocorruptio of data, oduplicatio of messages, oloss of messages), it ca cause liveess problems. I particular if oe thread is waitig for aother to do somethig, ad the other thread is waitig for the first to do somethig, the we have deadlock 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 17

Deadlock Example Suppose we have a bak accout class class Accout { private it balace = 0.0; public sychroized void addfuds( it amout ) { balace += amout; public void trasfer ( it amout, Accout toaccout ) { if( balace >= amout ) { toaccout.addfuds( amout ); balace -= amout; else { 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 18

Deadlock Example There is a subtle problem here. The itet is that oe should ot be able to trasfer out of a accout more moey tha it has. (A safety problem.) But, if two threads attempt to trasfer from the same accout at about the same time, the they might both succeed, eve though the fial balace will be egative. To fix this, we make trasfer sychroized. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 19

Deadlock Example class Accout { private it balace = 0; public sychroized void addfuds( it amout ) { balace += amout; public sychroized void trasfer (it amout, Accout toaccout){ if( balace >= amout ) { toaccout.addfuds( amout ); balace -= amout; else { 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 20

Deadlock Example But ow deadlock is possible. Suppose thread 0 tries to trasfer from accout x to accout y. At roughly the same time thread 1 attempts to trasfer from accout y to accout x 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 21

A possible seuece Thread 0 calls x.trasfer(100, y) obtais a lock o x calls y.addfuds() waits for lock o y waits for lock o y waits for lock o y waits for lock o y ad ifiitum Thread 1 calls y.trasfer( 50, x ) obtais a lock o y calls x.addfuds() waits for lock o x waits for lock o x waits for lock o x waits for lock o x ad ifiitum The Threads are ow deadlocked! 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 22

A solutio to deadlock Oe solutio is to always lock objects i a particular order. e.g. give each lockable object a globally uiue # The followig example uses sychroized blocks public void trasfer ( it amout, Accout toaccout ) { boolea choice = this.serialnum() <= toaccout. serialnum() ; sychroized( choice? this : toaccout ) { sychroized( choice? toaccout : this ) { if( balace >= amout ) { toaccout.addfuds( amout ) ; balace -= amout; else { 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 23

Testig Cocurret Programs You ca ot effectively test cocurret programs to show that they are error free Because of race coditios, a test may pass millios of times by chace. Tests that fail are useful. They tell us we have a bug. Tests that pass oly tell us that it is possible for the code to compute the correct result. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 24

Testig: A True Story I wated to illustrate how race coditios ca cause data corruptio. So I wrote a program with two threads sharig a it variable x. Iitially x was set to 0 Oe thread icremeted x a thousad times The other thread decremeted x a thousad times. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 25

Testig: A True Story class Icremetor exteds Thread { public void ru() { for(it i=0 ; i < 1000; ++i) ++x ; class Decremetor exteds Thread { public void ru() { for(it i=0 ; i < 1000; ++i) --x ; 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 26

Testig: A True Story I ra the two threads cocurretly System.out.pritl( "The iitial value of x is: " + x ) ; Thread p = ew Icremetor() ; Thread = ew Decremetor() ; p.start() ;.start(); // Wait for threads to fiish usig jois (ot show) System.out.pritl( "After "+1000+" icremets ad "+1000+" decremets" ) ; System.out.pritl( "the fial value of x is: " + x ) ; What do you thik happeed? 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 27

Testig: A True Story Here s the output: The iitial value of x is: 0 After 1000 icremets ad 1000 decremets the fial value of x is: 0 Eve though I deliberately wrote a faulty program ad gave it 1 thousad chaces to fail the test, it passed ayway.. I tried 10,000. The 100,000. Same result 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 28

Testig: A True Story Ad why did it pass? The JVM happeed to give each thread time slices so log that the icremetig thread completed its 100,000 icremets before the mai thread had a chace to eve start the decremetig thread. Chagig to 1,000,000 icremets ad decremets revealed the bug. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 29

Testig: A True Story I had falle victim to optimism. I had optimistically assumed that such a obvious bug would cause a thorough test to fail. If tests desiged to reveal blatat bugs, which we kow are i the program, fail to reveal them, should we expect testig to reliably reveal subtle bugs we do ot kow about? 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 30

If you ca t test, the what? The good ews is that although testig is isufficiet to reveal bugs, there are desig ad aalysis techiues that allow you to prove your programs correct. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 31

Cocurrecy What every computer egieer eeds to kow about cocurrecy: Cocurrecy is to utraied programmers as matches are to small childre. It is all too easy to get bured. Race coditios Deadlock Isufficiecy of testig 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 32

Summary of termiology cocurrecy: multiple agets ruig at the same time, iteractig thread: a idepedet path of cotrol Thread: a Java class represetig threads race coditio: a hazard caused by the upredictability of executio timig sychroized access: lockig of objects to obtai exclusive access wait ad otifyall: threads may wait util otified deadlock: a cycle of threads mutually waitig for each other safety property: a property that says somethig (bad) will ever happe liveess property: a property that says somethig (good) will evetually happe 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 33