1 Announcements HW8 due today HW9: Java Concurrency Will post tonight Due May 9 th Java infrastructure Download the Eclipse IDE Rainbow grades HW 1-6 Exam.

Slides:



Advertisements
Similar presentations
Threads, SMP, and Microkernels
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Concurrency The need for speed. Why concurrency? Moore’s law: 1. The number of components on a chip doubles about every 18 months 2. The speed of computation.
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.
Concurrency 101 Shared state. Part 1: General Concepts 2.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Designing a thread-safe class  Store all states in public static fields  Verifying thread safety is hard  Modifications to the program hard  Design.
Computer Systems/Operating Systems - Class 8
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
Intro to Threading CS221 – 4/20/09. What we’ll cover today Finish the DOTS program Introduction to threads and multi-threading.
DISTRIBUTED AND HIGH-PERFORMANCE COMPUTING CHAPTER 7: SHARED MEMORY PARALLEL PROGRAMMING.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Concurrency CS 510: Programming Languages David Walker.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Organization of Programming Languages-Cheng (Fall 2004) Concurrency u A PROCESS or THREAD:is a potentially-active execution context. Classic von Neumann.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
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.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
© 2009 Matthew J. Sottile, Timothy G. Mattson, and Craig E Rasmussen 1 Concurrency in Programming Languages Matthew J. Sottile Timothy G. Mattson Craig.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
111 © 2002, Cisco Systems, Inc. All rights reserved.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
1 (Worker Queues) cs What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.
Games Development 2 Concurrent Programming CO3301 Week 9.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
Synchronizing threads, thread pools, etc.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
Java Thread and Memory Model
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.
Multiprocessor Cache Consistency (or, what does volatile mean?) Andrew Whitaker CSE451.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
CMSC 330: Organization of Programming Languages Threads.
Fall 2008Programming Development Techniques 1 Topic 20 Concurrency Section 3.4.
Threads. Readings r Silberschatz et al : Chapter 4.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
1 Announcements HW9: Java Concurrency Due Monday May 9 th Tests in HW Server coming up Check your grades in Rainbow Grades! Homework Server survey:
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
Principles of Software Development
A brief intro to: Parallelism, Threads, and Concurrency
Multithreading / Concurrency
Thread Pools (Worker Queues) cs
Thread Pools (Worker Queues) cs
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Thread Implementation Issues
Threads Chapter 4.
Background and Motivation
Concurrency, Processes and Threads
Chapter 4: Threads & Concurrency
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
Threads and Multithreading
CMSC 202 Threads.
Threads and concurrency / Safety
Presentation transcript:

1 Announcements HW8 due today HW9: Java Concurrency Will post tonight Due May 9 th Java infrastructure Download the Eclipse IDE Rainbow grades HW 1-6 Exam 1-2 Quiz 1-7 Questions/concerns, contact us ASAP Spring 16 CSCI 4430, A Milanova/BG Ryder

Announcements HW9 is to be done in pairs Pair programming If you don’t have a partner, me Do so by Monday, May 2 nd at noon. Requests received later than that will remain unanswered Spring 16 CSCI 4430, A Milanova/BG Ryder 2

3 Last Class Object-oriented programming languages Benefits of object orientation Subtype polymorphism and dynamic method binding Polymorphism Subtype polymorphism Parametric polymorphism Ad-hoc polymorphism refers to function and operator overloading

4 Topics Intro to Concurrency Concurrency in Java Threads What can go wrong with threads? Synchronized blocks The Executor framework Java Programming Assignment

5 Intro to Concurrency and Concurrency in Java Read: Scott, Chapter

6 Concurrency Concurrent program Any program is concurrent if it may have more than one active execution context --- more than one “thread of control” Concurrency is everywhere A multithreaded web browser An IDE which compiles while we edit What are the main reasons for the (renewed) interest in concurrency in programming languages?

Concurrency and Parallelism Concurrent characterizes a system in which two or more tasks may be underway (at any point of their execution) at the same time A concurrent system is parallel if more than one task can be physically active at once This requires more than one processor 7

Parallelism in Software Systems Arises at different granularity From simple and small tasks, to large and complex tasks Instruction-level parallelism (ILP) Vector parallelism Essentially, like Scheme’s map Thread-level parallelism Tasks are now arbitrarily complex; concurrency is no longer hidden from programmer 8

Multiprocessor Machines Two broad categories of parallel architectures Shared-memory machines Those in which processors share common memory Non-shared-memory machines Those in which processors must communicate with messages 9

What Exactly is a Processor? For 30+ years, it used to be the single chip with a CPU, cache and other components Now, it can mean a single “device” with multiple chips; each chip can have multiple cores; each core can have multiple hardware threads. Also, subsets of the cores can share different levels of cache 10

What Exactly is a Processor? Usually OS and programming languages abstract away hardware complexity For us, programmers, ”processor” means a task/thread of computation Or the hardware that runs the thread of computation But as we saw many times in this class, abstraction (i.e., improved programmability) comes at a price. What is the price here? 11

12 Fundamentals of Concurrent Programming Two programming models for concurrency Shared memory Some program variables are accessible to multiple threads --- threads have shared state Threads communicate (interact) through shared state Message passing Threads have no shared state One thread performs explicit send to transmit data to another

13 Communication More formally, refers to any mechanism that allows one thread to obtain information produced by another thread Explicit in message passing models Implicit in shared memory models Synchronization Refers to any mechanism that allows the programmer to control the relative order of operations that occur Implicit in message passing models Explicit and necessary in shared memory models Fundamentals of Concurrent Programming

14 Shared Memory Model Programming language support for the shared memory model Explicit support for concurrency E.g., Java, C#: explicit threads, locks, synchronization, etc. Libraries C/C++: The POSIX #include Many types, macros and routines for threads, locks, other synchronization mechanisms We will take a closer look at Java

15 Threads Java has explicit support for multiple threads Two ways to create new threads: Extend java.lang.Thread Override “run()” method Implement Runnable interface Include a “run()” method in your class Starting a thread new MyThread().start(); new Thread(runnable).start(); Abstracted away by Executor framework

16 Terminology Concurrent programming with shared memory is about managing shared mutable state Shared state – memory locations that can be accessed by multiple threads Mutable state – the value of a location could change during its lifetime Atomic action – action that executes on the machine as a single indivisible operation E.g., read the value of variable i is atomic E.g., write the value of variable i is atomic E.g., i++ is not atomic

Java Programming Assignment Atomic transactions execute “at once” For example, transaction A=B+C; B=A+B must be executed “at once” (informally, it cannot be interrupted in the middle by another transaction) Accounts A: 0, B: 1, C: 2 Transaction 1:Transaction 2: A = B + C ; B = A + BB = C + B What are the acceptable outcomes? 17

18 What Can Go Wrong? class Account { int balance = 0; void deposit (int x) { this.balance = this.balance + x; } class AccountTask implements Runnable { public void run() { Main.act.deposit(10); } public class Main { static Account act = new Account(); public static void main(String args[]) { new Thread(new AccountTask()).start(); // Thread A new Thread(new AccountTask()).start(); // Thread B } Account object is shared mutable state.

19 What Can Go Wrong? Thread A:Thread B: r1 = act.balance r1 += 10 act.balance = r1r2 = act.balance r2 += 10 act.balance = r2

20 A common bug: Race Condition New types of bugs occur in concurrent programs; race conditions are the most common A data race (a type of race condition) occurs when two threads can access the same memory location “simultaneously” and at least one access is a write x=0if (x!=0) Thread A:Thread B: true-part

21 A common bug: Race Condition Check-and-act data race (common data race) if (instance==null) instance = new … public class LazyInitRace { private ExpensiveObject instance = null; public ExpensiveObject getInstance() { if (instance == null) instance = new ExpensiveObject(); return instance; } Thread A:Thread B: The two callers (in thread A and thread B) could receive different instances although there should be only one instance! if (instance==null) instance = new …

22 synchronized Block One mechanism to control the relative order of thread operations and avoid race conditions, is the synchronized block Use of synchronized : synchronized ( lock ) { // Read and write of shared state } lock is a reference to an object Critical section

23 synchronized Method One can also declare a method as synchronized: synchronized int m(String x) { // blah blah blah } equivalent to: int m(String x) { synchronized (this) { // blah blah blah }

24 synchronized Blocks Every Java object has a built-in intrinsic lock A synchronized block has two parts A reference to an object that serves as the lock Block of code to be guarded by this lock The lock serves as a mutex (or mutual exclusion lock) Only one thread can hold the lock If thread B attempts to acquire a lock held by thread A, thread B must wait (or block) until thread A releases the lock

25 Using Synchronized Blocks How do we make the Account class “safe”? synchronized void deposit(int x) { … } Thread A: synchronized (this) { r1 = balance r1 += 10 balance = r1Thread B: }synchronized (this) { r2 = balance r2 += 10 balance = r2 } this refers to global Account object

26 Using Synchronized Blocks Synchronized blocks help avoid data races Granularity of synchronized blocks Synchronized blocks that are too long (i.e., coarse grained locking) sacrifice concurrency and may lead to slowdown Force sequential execution as threads wait for locks Synchronized blocks that are too short (i.e., fine grained locking) may miss data races! Synchronization can cause deadlock!

27 Question In the code example below, does lock guarantee that no two threads ever execute the critical section “simultaneously”? synchronized ( lock ) { // Read and write of shared state }

28 Question Sequential code: List data = new ArrayList(); if (!data.contains(p)) { data.add(p); } Concurrent code, shared mutable state data: List data = new ArrayList() created in main thread if (!data.contains(p)) { data.add(p); } is executed by multiple threads

29 Implementing data safely: One attempt is to use the Synchronized Collections (since Java 1.2) Created by Collections.synchronizedXYZ methods E.g., List data = Collections.synchronizedList (new ArrayList()); All public methods are synchronized on this Even if data is a synchronized List, code still not right. What can go wrong?

30 Concurrent Collections (since Java 1.5) E.g., ConcurrentHashMap Implement different, more efficient (concurrent) synchronization mechanism Provide additional atomic operations E.g., putIfAbsent(key, value) Provide weak iterators – i.e., iterators that do not throw ConcurrentModificationException Implementing data safely:

31 So… What Can Go Wrong? New types of bugs occur in concurrent programs Race conditions Atomicity violations Deadlocks There is nondeterminism in concurrency, which makes reasoning about program behavior extremely difficult Spring 16 CSCI 4430, A Milanova

32 Topics Intro to Concurrency Concurrency in Java Threads What can go wrong with threads? Synchronized blocks The Executor framework Java Programming Assignment

33 Organizing Concurrent Applications One way to organize concurrent programs: Organize program into tasks Identify tasks and task boundaries Tasks should be as independent of other tasks as possible Ideally, tasks do not depend on mutable shared state and do not write mutable shared state If there is mutable shared state, tasks should be synchronized appropriately! Each task should be a relatively small portion of the total work In you Java assignment, what is the task?

34 Sequential Task Execution A Web Server What problems do you see here? public class SingleThreadedWebServer { public static void main(String[] args) throws IOException { ServerSocket socket = new ServerSocket(80); while (true) { Socket connection = socket.accept(); handleRequest(connection); }

35 Explicit Threads for Task Execution public class ThreadPerTaskWebServer { public static void main(String[] args) throws IOException { ServerSocket socket = new ServerSocket(80); while (true) { Socket connection = socket.accept(); Runnable task = new Runnable() { public void run() { handleRequest(connection); } }; new Thread(task).start(); }

36 The Executor Framework Part of java.util.concurrent (Java 1.5) Flexible thread pool implementation High-level abstraction: Executor, not Thread Decouples task submission from task execution E.g., Executor e managers a thread pool of 3 threads ABC e.g., Executor e = … e.execute(t1); e.execute(t2); e.execute(t3); e.execute(t4); e.execute(t5); e.execute(t6); time t1t2 t3 t4 t5 t6 task submission task execution

37 Using Executor for Task Execution public class TaskExectorWebServer { private … Executor e = Excutors.newFixedThreadPool(3); public static void main(String[] args) throws IOException { ServerSocket socket = new ServerSocket(80); while (true) { Socket connection = socket.accept(); Runnable task = new Runnable() { public void run() { handleRequest(connection); } }; e.execute(task); }

38 Your Java Programming Assignment Class WorkerTask becomes a Runnable In runServer, create an Executor (a thread pool) Send new runnable WorkerTask objects using execute to the thread pool One task processes one transaction (i.e., one line from input file). I.e., the task is one transaction

39