CMPSCI 187: Programming With Data Structures. Lecture #20: Concurrency and a Case Study David Mix Barrington 24 October 2012

Similar documents
CmpSci 187: Programming with Data Structures Spring 2015

CMPSCI 187: Programming With Data Structures. Lecture #21: Array-Based Lists David Mix Barrington 26 October 2012

CMPSCI 187: Programming With Data Structures. Lecture #23: Linked Lists David Mix Barrington 31 October 2012

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Note: Each loop has 5 iterations in the ThreeLoopTest program.

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Exercise Session Week 8

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 8. Nadia Polikarpova

Exercise Session Week 8

Java s Implementation of Concurrency, and how to use it in our applications.

THREADS AND CONCURRENCY

Contents. 6-1 Copyright (c) N. Afshartous

Threads and Parallelism in Java

CS 351 Design of Large Programs Threads and Concurrency

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

CMSC 132: Object-Oriented Programming II

Lecture 35. Threads. Reading for next time: Big Java What is a Thread?

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

CMPSCI 187: Programming With Data Structures. Lecture 6: The StringLog ADT David Mix Barrington 17 September 2012

Let s go straight to some code to show how to fire off a function that runs in a separate thread:

Lecture 9: Introduction to Monitors

1.00 Lecture 29. Sensors and Threads. Reading for next time: Numerical Recipes (online)

Multiple Inheritance. Computer object can be viewed as

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

What is a thread anyway?

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

Java Barrier Synchronizers: CountDownLatch (Part 1)

Introduction to Computing II (ITI 1121) Final Examination

CMPSCI 187: Programming With Data Structures. Lecture #15: Thinking Recursively David Mix Barrington 10 October 2012

CSC 1052 Algorithms & Data Structures II: Queues

Concurrent Programming using Threads

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Outline of lecture. i219 Software Design Methodology 10. Multithreaded programming. Kazuhiro Ogata (JAIST)

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Multithreaded Programming

Threads Chate Patanothai

Exam Number/Code : 1Z Exam Name: Name: Java Standard Edition 6. Demo. Version : Programmer Certified Professional Exam.

Shared Objects & Mutual Exclusion

Michele Van Dyne MUS 204B Concurrency Issues

Virtual Machine Design

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

CPS221 Lecture: Threads

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Problems with Concurrency. February 19, 2014

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Programming Language Concepts: Lecture 11

Java Threads. Introduction to Java Threads

Concurrency. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

Concurrent Computing CSCI 201 Principles of Software Development

CMPSCI 187: Programming With Data Structures. Lecture #16: Thinking About Recursion David Mix Barrington 12 October 2012

Synchronization

Concurrency. Fundamentals of Computer Science

CMSC 330: Organization of Programming Languages

Object Oriented Programming. Week 10 Part 1 Threads

CSCD 330 Network Programming

Java Threads. COMP 585 Noteset #2 1

Project. Threads. Plan for today. Before we begin. Thread. Thread. Minimum submission. Synchronization TSP. Thread synchronization. Any questions?

CMPSCI 187: Programming With Data Structures. Lecture #24: Files and a Case Study David Mix Barrington 2 November 2012

THIS EXAMINATION PAPER MUST NOT BE REMOVED FROM THE EXAMINATION ROOM

i219 Software Design Methodology 11. Software model checking Kazuhiro Ogata (JAIST) Outline of lecture

Lecture 8: September 30

COMP 213. Advanced Object-oriented Programming. Lecture 23. Shared Variables and Synchronization

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

Array Based Lists. Collections

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

System Programming. Practical Session 4: Threads and Concurrency / Safety

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Java Programming Lecture 23

CSCD 330 Network Programming

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

Favoring Isolated Mutability The Actor Model of Concurrency. CSCI 5828: Foundations of Software Engineering Lecture 24 04/11/2012

Concurrency and Java Programming

Only one thread can own a specific monitor

CS11 Java. Fall Lecture 7

Synchronization SPL/2010 SPL/20 1

Synchronized Methods of Old Versions of Java

Exam Concurrent and Real-Time Programming

Introduction to Java Threads

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

An Introduction to Programming with Java Threads Andrew Whitaker University of Washington 9/13/2006. Thread Creation

EECS2030 Week 7 worksheet Tue Feb 28, 2017

Object Oriented Software Design

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer.

Concurrent Programming Lecture 10

F I N A L E X A M I N A T I O N

Synchronization. Announcements. Concurrent Programs. Race Conditions. Race Conditions 11/9/17. Purpose of this lecture. A8 released today, Due: 11/21

LECTURE OBJECTIVES 6-2

CS 326: Operating Systems. Process Execution. Lecture 5

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Transcription:

CMPSCI 187: Programming With Data Structures Lecture #20: Concurrency and a Case Study David Mix Barrington 24 October 2012

Concurrency and a Case Study Concurrency and Threads Example: Counter, Increase, and Runnable Interference of Threads and Synchronization Synchronized Queues and the java.util Queues The Average Waiting Time Case Study A Simulation With Randomized Customers The GlassQueue Class

Concurrency and Threads Computers are capable of doing more than one thing at the same time. Some computer hardware can actually have two processes going on simultaneously using different processors. Other computers use timesharing, a system where a single processor moves among more than one task, making progress on each in turn and maintaining the context of each. The more programs interact with the real world, in such ways as monitoring sensors or awaiting commands, the more important concurrency becomes. In this lecture we ll take a brief look at some of the major issues with concurrency, and the facilities Java offers to deal with them. You ll learn much more about concurrency in CMPSCI 230 and 377.

Example: Counter, Increase, and Runnable The Runnable interface requires a class to have a public method named run, which can then be put into a Thread object. This allows it to be executed in another thread of the Java execution besides the main thread. public class Counter { private int count; public Counter( ) {count = 0;} public void increment( ) {count++;} public void tostring( ) {return Count is:\t + count;}} public class Increase implements Runnable { private Counter c; private int amount; public Increase (Counter c, int amount) { this.c = c; this.amount = amount;} public void run( ) { for (int i = 1; i <= amount; i++) c.increment;}}

The Thread created below should eventually increase the value stored in c to 1000. But DJW report that when they ran this, they got varying answers under 100. The reason is that they start running the thread and then in parallel ask the main thread to print the value stored in c. They find out how far the thread t has gotten by the time the main thread asks c its value. Note that Thread objects can throw InterruptedException objects, which are checked and thus must be caught or thrown beyond the main method. public class Demo02 { public static void main (String[ ] args) throws InterruptedException { Counter c = new Counter( ); Runnable r = new Increase(c, 1000); Thread t = new Thread(r); t.start( ); System.out.println(c);}} // note typo on p. 343

Interference of Threads and Synchronization DJW continue with more similar examples. In Demo03, they start the thread t and then say t.join( ), which causes the main thread to wait until t is finished. In this case they get the value of 1000 stored in c. In Demo04 they create two threads, t1 and t2, each of which should increase c by 5000. They then start both and ask both to join. This should set c to 10000, but they get varying values in the 9000 s. The problem is interference between the threads. Each thread is reading the value in c, increasing it by 1, and writing it back. Suppose t1 writes its value between t2 s read and write operations? Its value is overwritten by t2 s, and the increase t1 wanted to make is never made. In Demo05 they replace the Counter with a SyncCounter in which the increment method is public synchronized void increment. Then only one thread can have access to the method at a time, and we get 10000.

Synchronized Queues and the java.util Queues DJW s Demo06 creates an ArrayBndQueue<Integer> object, loads it with the numbers from 1 to 100, then creates two threads with variants of the Increase class that are each to take numbers from the queue and add their value into the SyncCounter. Though the counter is synchronized, the queue is not. They find that intermittently, a number will not be added in or an exception will occur. This apparently was because the two threads tried to dequeue at about the same time. They then build a SyncArrayBndQueue class that is synchronized. The Java library contains both synchronized (thread-safe) and unsynchronized classes among the nine implementations of the Queue interface. The older Java classes like Vector are thread-safe, but modern Java offers cheaper, simpler unsynchronized classes like ArrayList, that may be used in the many situations where there aren t competing threads, as well as specific thread-safe classes.

The Average Waiting Time Case Study Queues are perhaps most commonly used to simulate real-world waiting situations. DJW offer a case study that allows a user to compare the average waiting times for customers with varying numbers of servers available, each with its own queue. More servers cost more money, but customers are happier with shorter waiting times. Exploring the tradeoffs in simulation is probably cheaper than running real-world experiments. But the simulation is only as accurate as its model of customer and server behavior. Different customers take different amounts of time to be served, and arrive for service with different intervals of time between them. An easy way to get such a variation is to use a Random object to generate numbers uniformly, within given ranges, for the service times and the intervals. The ranges might be based on actual experimental data -- DJW s simulator allows the user to provide the max and min service times and interval times.

A Simulation With Randomized Customers The Simulation class will create the customers and queues, run the experiment, and report the results. A Customer object has an arrival time and a service time on its creation, and will have a finish time assigned during the simulation. We can calculate how long the Customer was waiting in the queue -- our output will be the average waiting time for the set of customers. We ll need a CustomerGenerator class to generate these objects using a Random object. A Queue will correspond to a server. When a customer arrives, she will enter the shortest available queue (in terms of number of customers in it). The server will dequeue a customer when it finishes with the previous one, unless its queue is empty. The dequeued customer gets a finish time, computed by adding her service time to the time she is dequeued. But our existing queue classes actually won t suffice to model the queues we want.

The GlassQueue Class We have here a nice example of the use of inheritance. In the case study, we want a queue that is able to report its size and to give pointers to its front and rear elements. (DJW call this a glass queue because it is transparent.) Rather than write a new class repeating the old code, we can extend the existing class. Note that the new constructor calls the old one using the word super. This process repeats through the inheritance hierarchy all the way to the Object constructor. The zero-parameter constructor exists even if not declared. public class GlassQueue<T> extends ArrayUnbndQueue<T> { public GlassQueue( ) {super( );} public GlassQueue(int origcap) {super(origcap);} public int size( ) {return numelements;} public T peekfront( ) {return queue[front];} public T peekrear( ) {return queue[rear];}}