David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 16: Concurrent Programming.

Slides:



Advertisements
Similar presentations
Class 18: Concurrency on Mars Fall 2010 UVa David Evans cs2220: Engineering Software Image: Michael Dewey-Vogt.
Advertisements

– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 13: Concurring Concurrently.
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 6: Reasoning about Data Abstractions.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Thread synchronization Example:Producer/Consumer Relationship Buffer –Shared memory region Producer thread –Calls produce method to add item to buffer.
Threads Daniel Bennett, Jeffrey Dovalovsky, Dominic Gates.
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads Just Java: C10–pages 251- C11–pages 275-
© Amir Kirsh Threads Written by Amir Kirsh. 2 Lesson’s Objectives By the end of this lesson you will: Be familiar with the Java threads syntax and API.
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.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
Class 14: Object-Oriented Programming Fall 2010 University of Virginia David Evans cs2220: Engineering Software.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass.
Cs205: engineering software university of virginia fall 2006 David Evans Substitution Principle.
Type Abstraction SWE Spring October 05Kaushik, Ammann Substitution Principle “In any client code, if supertype object is substituted.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Multithreading in JAVA
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
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.
Java 3: Odds & Ends Advanced Programming Techniques.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Multithreading Lec 23.
Multi Threading.
Multithreading.
Locking cs205: engineering software
Section 5.7 Concurrency, Interference, and Synchronization.
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Subtyping Rules David Evans cs205: engineering software BlackBear
Concurrency without Locks
Lecture 15: Concurring Concurrently CS201j: Engineering Software
Concurring Concurrently
Condition Variables and Producer/Consumer
Concurrency and Threads
Condition Variables and Producer/Consumer
Java Based Techhnology
Multithreading.
Lecture 4: Data Abstraction CS201j: Engineering Software
Computer Science 2 06A-Java Multithreading
Multithreading in java.
Threads and Multithreading
Lecture 13: Subtyping Rules Killer Bear Climber
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Threads and concurrency / Safety
Presentation transcript:

David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 16: Concurrent Programming

28 October 2003CS 201J Fall Menu Section Problems Concurrent Programming

28 October 2003CS 201J Fall StringSet <= StringBag ? Signature rule: subtype must implement all supertype methods –StringBag has method public int count (String s) not implemented by StringSet Signature rule is violated: StringSet is not a behavioral subtype of StringBag. Example: The statement s.count (“test”); is possible if s is a StringBag, but not if s is a StringSet.

28 October 2003CS 201J Fall StringSet <= StringBag ? Methods rule: precondition of subtype method must be weaker than precondition of supertype method StringBag: public void insert (String s) // REQUIRES: true StringSet: public void insert (String s) // REQUIRES: s is not an element of this. true  s is not an element of this

28 October 2003CS 201J Fall StringBag <= StringSet Signature Rule –StringBag implements all StringSet methods –Argument types contravariant (all are same), result types covariant (all are same), no new exceptions Methods Rule –Preconditions are weaker insert: s is not an element of this  true

28 October 2003CS 201J Fall StringBag <= StringSet Methods Rule: Postconditions are stronger StringBag () post  StringSet () post StringBag.insert post  StringSet.insert post “Adds s to this” StringBag.isIn post  StringSet.isIn post “Returns true if there is at least one s in this”  “Returns true if s is an element of this” StringBag.size post  StringSet.size post “Returns the total number of elements in this”  “Returns the number of elements in this”

28 October 2003CS 201J Fall StringBag <= StringSet Properties Rule: subtype preserves supertype properties StringSet: // OVERVIEW: StringSets are unbounded, mutable sets of Strings. StringBag: // OVERVIEW: StringBags are unbounded, // mutable bags (unordered, but the same // String may appear multiple times) of // Strings.

28 October 2003CS 201J Fall Programming Concurrency Java API: Thread –A separate execution thread –Methods Thread (Runnable r) Create a thread. The thread’s run method will invokve r.run (). start () REQUIRES: this is not already started Schedule the thread to run. The VM will start the thread and involve run ().

28 October 2003CS 201J Fall Yarn public class Yarn extends Thread { public void run () { System.err.println ("Running thread: " + currentThread ()); } public static void main (String args[]) { Yarn y = new Yarn (); y.start (); System.err.println ("Done: " + currentThread ()); } What could this produce?

28 October 2003CS 201J Fall Yarn public class Yarn extends Thread { public void run () { System.err.println ("Running thread: " + currentThread ()); } public static void main (String args[]) { Yarn y = new Yarn (); y.start (); System.err.println ("Done: " + currentThread ()); } Done: Thread[main,5,main] Running thread: Thread[Thread-0,5,main] Actual output: Running thread: Thread[Thread-0,5,main] Done: Thread[main,5,main] Plausible output:

28 October 2003CS 201J Fall Yarn public class Yarn extends Thread { public void run () { while (true) { System.err.println ("Running thread: " + currentThread ()); } public static void main (String args[]) { Yarn y = new Yarn (); y.start (); System.err.println ("Done: " + currentThread ()); } Done: Thread[main,5,main] Running thread: Thread[Thread-0,5,main]

28 October 2003CS 201J Fall class Counter { private int count; public Counter () { count = 0; } public void increment () { count++; } public void decrement () { count--; } public int getValue () { return count; } } class IncThread extends Thread { private Counter c; public IncThread (Counter p_c) { c = p_c; } public void run () { while (true) { c.increment (); System.err.println ("Running inc thread: " + currentThread () + " / Value: " + c.getValue ()); }

28 October 2003CS 201J Fall class DecThread extends Thread { private Counter c; public DecThread (Counter p_c) { c = p_c; } public void run () { while (true) { c.decrement (); System.err.println ("Running dec thread: " + currentThread () + " / Value: " + c.getValue ()); } public class Yarn { public static void main (String args[]) { Counter c = new Counter (); IncThread ithread = new IncThread (c); DecThread dthread = new DecThread (c); ithread.start (); dthread.start (); }

28 October 2003CS 201J Fall Running inc thread: Thread[Thread-0,5,main] / Value: 1 Running inc thread: Thread[Thread-0,5,main] / Value: 2 Running inc thread: Thread[Thread-0,5,main] / Value: 3 Running inc thread: Thread[Thread-0,5,main] / Value: 4 … Running inc thread: Thread[Thread-0,5,main] / Value: 61 Running inc thread: Thread[Thread-0,5,main] / Value: 62 Running dec thread: Thread[Thread-1,5,main] / Value: 61 Running inc thread: Thread[Thread-0,5,main] / Value: 62 Running dec thread: Thread[Thread-1,5,main] / Value: 61 Running inc thread: Thread[Thread-0,5,main] / Value: 62 Running dec thread: Thread[Thread-1,5,main] / Value: 61 Running dec thread: Thread[Thread-1,5,main] / Value: 60 … Running dec thread: Thread[Thread-1,5,main] / Value: 1 Running dec thread: Thread[Thread-1,5,main] / Value: 0 Running dec thread: Thread[Thread-1,5,main] / Value: -1 Running inc thread: Thread[Thread-0,5,main] / Value: -1 Running inc thread: Thread[Thread-0,5,main] / Value: 0 Running inc thread: Thread[Thread-0,5,main] / Value: 1 Running inc thread: Thread[Thread-0,5,main] / Value: 2 …

28 October 2003CS 201J Fall Any race conditions? class IncThread extends Thread { private Counter c; public IncThread (Counter p_c) { c = p_c; } public void run () { while (true) { c.increment (); System.err.println ("Running inc thread: " + currentThread () + " / Value: " + c.getValue ()); }

28 October 2003CS 201J Fall public void run () { while (true) { synchronized (c) { c.increment (); System.err.println ("Running inc thread: " + currentThread () + " / Value: " + c.getValue ()); } public void run () { synchronized (c) { while (true) { c.increment (); System.err.println ("Running inc thread: " + currentThread () + " / Value: " + c.getValue ()); } Option 1 Option 2 Would hold the lock on c forever!

28 October 2003CS 201J Fall Banking How can we ensure that the counter value never goes negative?

28 October 2003CS 201J Fall Priorities In general, threads with higher priorities will be scheduled preferentially. There are no guarantees Thread void setPriority (int newPriority) // MODIFIES: this // EFFECTS: Changes the priority of this // thread to newPriority.

28 October 2003CS 201J Fall Priorities, Priorities ithread.setPriority (Thread.NORM_PRIORITY); ithread.start (); dthread.setPriority (Thread.MIN_PRIORITY); dthread.start (); The ithread should run more than the dthread, but there is no guarantee.

28 October 2003CS 201J Fall wait and notify Thread A synchronized (o) o.wait () Thread B synchronized (o) { o.notify () } // end synchronized can reclaim o lock

28 October 2003CS 201J Fall public class Object { public final void wait () throws InterruptedException // REQUIRES: Current thread holds the // lock for this. // EFFECTS: Causes the current thread // to release the lock and wait until // notify is called on this. If the thread // is interrupted throws InterruptedException public final void notify () // EFFECTS: If any threads are waiting on this // object, chooses one of those threads to // awaken. final means subtypes may not override this method

28 October 2003CS 201J Fall class IncThread extends Thread { private Counter c; public IncThread (Counter p_c) { c = p_c; } public void run () { while (true) { synchronized (c) { c.increment (); System.err.println ("Running inc thread: " + currentThread () + …); c.notify (); } } } } class DecThread extends Thread { … public void run () { while (true) { synchronized (c) { if (c.getValue () <= 0) { try { c.wait (); } catch (InterruptedException e) { ; } } c.decrement (); System.err.println ("Running dec thread: " + …); } } } }

28 October 2003CS 201J Fall Counter c = new Counter (); IncThread ithread = new IncThread (c); DecThread dthread = new DecThread (c); ithread.setPriority (Thread.NORM_PRIORITY); ithread.start (); dthread.setPriority (Thread.MAX_PRIORITY); dthread.start (); Running inc thread: Thread[Thread-0,5,main] / Value: 1 Running dec thread: Thread[Thread-1,10,main] / Value: 0 Running inc thread: Thread[Thread-0,5,main] / Value: 1 Running dec thread: Thread[Thread-1,10,main] / Value: 0 Running inc thread: Thread[Thread-0,5,main] / Value: 1 Running dec thread: Thread[Thread-1,10,main] / Value: 0 Running inc thread: Thread[Thread-0,5,main] / Value: 1 Running dec thread: Thread[Thread-1,10,main] / Value: 0

28 October 2003CS 201J Fall Charge Thread work = new Thread (ps5); work.setPriority (Thread.MAX_PRIORITY); work.start ();