Threads and Concurrency in Java: Part 1

Similar documents
Threads and Concurrency in Java: Part 1

Threads and Concurrency in Java: Part 2

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

Threads and Concurrency in Java

Lecture 28: Data Link Layer

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

Chapter 4 Threads. Operating Systems: Internals and Design Principles. Ninth Edition By William Stallings

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

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

One advantage that SONAR has over any other music-sequencing product I ve worked

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

Chapter 4 The Datapath

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

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

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

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

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

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

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

Python Programming: An Introduction to Computer Science

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

Data diverse software fault tolerance techniques

Python Programming: An Introduction to Computer Science

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

Chapter 1. Introduction to Computers and C++ Programming. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

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

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

EE 459/500 HDL Based Digital Design with Programmable Logic. Lecture 13 Control and Sequencing: Hardwired and Microprogrammed Control

UNIVERSITY OF MORATUWA

Avid Interplay Bundle

Guide to Applying Online

Weston Anniversary Fund

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

10/23/18. File class in Java. Scanner reminder. Files. Opening a file for reading. Scanner reminder. File Input and Output

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

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

Chapter 5: Processor Design Advanced Topics. Microprogramming: Basic Idea

Elementary Educational Computer

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Appendix D. Controller Implementation

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

Module Instantiation. Finite State Machines. Two Types of FSMs. Finite State Machines. Given submodule mux32two: Instantiation of mux32two

IMP: Superposer Integrated Morphometrics Package Superposition Tool

End Semester Examination CSE, III Yr. (I Sem), 30002: Computer Organization

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

Multi-Threading. Hyper-, Multi-, and Simultaneous Thread Execution

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

Review: The ACID properties

CMPT 125 Assignment 2 Solutions

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

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

27 Refraction, Dispersion, Internal Reflection

Computers and Scientific Thinking

6.854J / J Advanced Algorithms Fall 2008

Architectural styles for software systems The client-server style

Greedy Algorithms. Interval Scheduling. Greedy Algorithms. Interval scheduling. Greedy Algorithms. Interval Scheduling

Lecture 1: Introduction and Strassen s Algorithm

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

Examples and Applications of Binary Search

Customer Portal Quick Reference User Guide

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

Software development of components for complex signal analysis on the example of adaptive recursive estimation methods.

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

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

Analysis of Algorithms

Chapter 8. Strings and Vectors. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Computer Science Foundation Exam. August 12, Computer Science. Section 1A. No Calculators! KEY. Solutions and Grading Criteria.

Graphs. Minimum Spanning Trees. Slides by Rose Hoberman (CMU)

Computer Architecture. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

CMSC Computer Architecture Lecture 3: ISA and Introduction to Microarchitecture. Prof. Yanjing Li University of Chicago

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

Guaranteeing Hard Real Time End-to-End Communications Deadlines

DEFINITION OF CELL BEHAVIOUR. Actions and Behaviour. CELL = a CELL CELL = b CELL

Using VTR Emulation on Avid Systems

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

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

COP4020 Programming Languages. Compilers and Interpreters Prof. Robert van Engelen

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

Investigation Monitoring Inventory

Τεχνολογία Λογισμικού

EE University of Minnesota. Midterm Exam #1. Prof. Matthew O'Keefe TA: Eric Seppanen. Department of Electrical and Computer Engineering

3.1 Overview of MySQL Programs. These programs are discussed further in Chapter 4, Database Administration. Client programs that access the server:

Data Structures and Algorithms. Analysis of Algorithms

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

Java net programming II

Evaluation scheme for Tracking in AMI

How do we evaluate algorithms?

Web OS Switch Software

condition w i B i S maximum u i

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 8. Strings and Vectors. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

MOTIF XF Extension Owner s Manual

Exercise Set: Implementing an Object-Oriented Design

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n))

Data Structures Week #9. Sorting

The Magma Database file formats

L6: FSMs and Synchronization

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

UH-MEM: Utility-Based Hybrid Memory Management. Yang Li, Saugata Ghose, Jongmoo Choi, Jin Sun, Hui Wang, Onur Mutlu

Transcription:

Cocurrecy Threads ad Cocurrecy i Java: Part 1 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. 1 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 2 Cocurrecy: What Cocurrecy: Where Cocurrecy meas that there are multiple agets ruig at the same time ad iteractig. Sources of cocurrecy: We have cocurrecy whe we have iteractig processes ruig o differet computers (e.g. Apache a web server o moa.egr.mu.ca ad Firefox a web browser o paradox.egr.mu.ca ) 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 3 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 4

Cocurrecy : Where Cocurrecy : Where We also have cocurrecy whe we have iteractig processes ruig o the same computer. E.g. Firefox ad Widows Explorer. Every iteractive program is part of a cocurret system: the user is a cocurret aget. Furthermore we ca have multiple threads of cotrol withi oe OS process. A.K.A. multithreadig Cocurrecy ca be itermachie, iterprocess, or multithreadig. These slides cocetrate o itraprocess cocurrecy i Java. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 5 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 6 Cocurrecy: Why Cocurrecy: Why Reasos for usig cocurrecy Speed. Multiple threads ca be ru o multiple processors (or multiple cores). This may give a speed advatage. Distributio. We may wish differet parts of a system to be located o differet machies for reasos of coveiece, security, reliability,. Reasos for usig cocurrecy Asychroy. It is easiest to deal with multiple sources of evets by havig oe thread dedicated to each stream of icomig or outgoig evets. For example, a web browser may use oe thread to deal with iput from the user ad oe thread for each server it is curretly iteractig with. Likewise a web server will typically dedicate at least oe thread to each curret sessio. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 7 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 8

Threads Each thread has its ow program couter registers local variables ad stack All threads share the same heap (objects) Cocurrecy: How Multiple processors Multiprocessor (ad mulitcore) CPU L1 Cache CPU L1 Cache 2 threads 2 processors time L2 Cache Cetral Memory 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 9 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 10 Cocurrecy: How Time slicig implemetatio 2 threads 1 processor Cotext switch Memory Previously The This Pipelie PC Iitially ad gree is a registers the coceptual saved PC drais thread red thread ow are view ad saved. executes register values are is of executig the process. fetched. Implemetatio details will vary. Sigle processor The CPU is switched betwee threads at upredictable times time Copy of PC Copy of registers PC Registers Copy of PC Copy of registers Multiple processor Thread may occasioally migrate 3 threads 2 processors Stack IF DEC EX WB CPU Stack time Istructios Heap 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 11 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 12

Thread Objects i Java I Java, threads are represeted by objects of class java.lag.thread. The ru method cotais the actual code for the thread to execute. public class ThreadExample exteds Thread { private Strig message; ThreadExample( Strig message ) { this.message = getname() + ": " + message; @Override public void ru() { for( it i=0 ; i<20 ; ++i ) System.out.pritl( message ); 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 13 Startig a ew thread Callig t.start() starts a ew thread which executes the t.ru() public class ThreadExampleMai { public static void mai(strig[] args) { ThreadExample thread0 = ew ThreadExample("Hi"); ThreadExample thread1 = ew ThreadExample("Ho"); thread0.start(); thread1.start(); System.out.pritl( Thread.curretThread().getName() + ": Mai is doe"); 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 14 Output for example Race Coditios Possible output for example: Thread-1: Ho Thread-1: Ho Thread-0: Hi mai: Mai is doe Thread-1: Ho Thread-0: Hi (ad so o for aother 35 lies) Whe t.ru() completes the thread stops. Whe all threads have stopped the program exits A system has a race coditio whe its correctess depeds the order of certai evets, but the order of those evets is ot sufficietly cotrolled by the desig of the system. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 15 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 16

Race Coditios Race Coditios Ofte race coditios occur because 2 agets have ucotrolled access to a shared resource Cosider two trai routes that share the same bridge A solutio: Before crossig the bridge, trais acuire a toke After crossig the bridge, trais reliuish the toke Acuire the toke Reliuish the toke Uless access to the shared resource is cotrolled, disaster may esue. Acuire the toke Reliuish the toke Oly Both As Iitially the soo oe trais other as ca try trai toke the succeed. toke ca acuire is free acuire is free at the the agai same toketime 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 17 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 18 Race Coditios i Software Remember: objects are shared by threads I Java, access to a object s methods is ucotrolled, by default!!!! Suppose we have public class Couter { private it cout = 0; public void icremet() { ++cout; System.out.pritl(cout); Race Coditios i Software Differet threads ca share the same Couter public class CouterThread exteds Thread { private Couter couter; CouterThread(Couter c) { this.couter = c; @Override public void ru() { for( it i=0 ; i<10 ; ++i ) { couter.icremet(); 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 19 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 20

Race Coditios i Software Execute the followig: public class CouterMai { public static void mai(strig[] args) { Couter c = ew Couter(); // Threads p ad share the same couter CouterThread p = ew CouterThread(c); CouterThread = ew CouterThread(c); p.start();.start(); Race Coditios i Software public class CouterMai { public static void mai(strig[] args) { Couter c = ew Couter(); CouterThread p = ew CouterThread(c); CouterThread = ew CouterThread(c); p.start();.start(); Possible Result: 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 WTF? 11 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 21 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 22 Race Coditios i Software The reaso is that the icremet operatio results i multiple bytecode (low-level JVM) istructios that ca get iterleaved Focus oly o ++cout public class Couter { private it cout = 0; Race Coditios i Software The statemet ++cout results i the followig bytecode load cout to r0 store r0 to cout public void icremet() { ++cout; System.out.pritl(cout); r0 represets register 0 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 23 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 24

Race Coditios i Software Two threads ivoke icremet at about the same time (Recall: Registers are local to the thread.) A race coditio results. p cout r0 (i p) r0 (i ) load cout to r0 store r0 to cout load cout to r0 store r0 to cout 41 42 42 41+1+1 = 42? Of the two icremets, oe was lost. 41 42 41 42 Race Coditios: Aother Example Cosider trasfers o a accout class AccoutMaager { private Accout savigs ; private Accout cheuig; public void trasfertosavigs( it amout ) { it s = savigs.getbalace() ; it c = checkig.getbalace() ; savigs.setbal( s+amout ) ; cheuig.setbal( c-amout ) ; Two threads execute trasfers. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 25 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 26 Race Coditios : Aother Example Oe Thread Aother Thread sav ch (amout = 500) (amout = 1000) s = savigs.getbalace() 3000 3000 c = cheuig.getbalace() s = savigs.getbalace() 4000 savigs.setbal(s+1000) 2000 cheuig.setbal(c-1000) c = cheuig.getbalace() 2000 3500 savigs.setbal(s+500) 1500 cheuig.setbal(c-500) sychroized to the rescue Methods may be declared sychroized public class Couter { private it cout = 0; public sychroized void icremet() { ++cout; System.out.pritl(cout); I started with $6000 ad eded with $5000. This is ot good. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 27 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 28

sychroized to the rescue sychroized to the rescue Each object has a associated toke called its lock. Whe a thread ivokes a sychroized method x.m(): At each poit i time, each lock either is owed by o thread or is owed by oe thread. If it does ot already ow the recipiet s (x s) lock, It waits util it ca acuire the lock A thread that attempts to acuire a lock must wait util o thread ows the lock. After acuirig a lock, a thread ows it util it reliuishes it. Oce the lock has bee acuired the thread begis to execute the method Whe a thread leaves a ivocatio of a sychroized method: If it is leavig the last sychoroized ivocatio for that object It reliuishes the lock as it leaves 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 29 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 30 sychroized to the rescue sychroized to the rescue Hece, for ay object x, at most oe thread may be executig ay of x s sychroized methods. Example: Two threads ivoke c.icremet() at about the same time. 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 31 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 32

sychroized to the rescue Thread p reuest lock o object c acuire lock o object c load cout to r0 store r0 to cout reliuish lock o object o Thread reuest lock o object o waits waits waits waits acuire lock o object o load cout to r0 store r0 to cout reliuish lock o object o 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 33 The Room metaphor Whe a thread Each has left all object is sychroized a room methods of the object, it leaves At the all room times, at most oe allowig thread is aother allowed thread to eter. to be i the room. The atechamber The room Whe a thread ot i the room calls a sychroized method, it eters a atechamber. There it waits util the room is empty. Whe the room is empty, at most oe thread is allowed to eter the room 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 34 Desig rule: Shared Objects Accouts For ay object that might be used by more tha oe thread at the same time Declare all methods that access or mutate the data sychroized (Note: private methods are exempted from this rule, as they ca oly be called oce a oprivate method has bee called.) Costructors eed ot (ad ca ot) be sychroized I AccoutMaager add sychroized to all methods class AccoutMaager { private Accout savigs ; private Accout cheuig; public sychroized void trasfertosavigs( it amout ) { it s = savigs.getbalace() ; it c = checkig.getbalace() ; savigs.setbal( s+amout ) ; cheuig.setbal( c-amout ) ; 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 35 2003--09 T. S. Norvell Memorial Uiversity Threads. Slide 36