COMPSCI 230 S2C 2013 Software Design and Construction Deadlock, Performance, Programming Guidelines Lecture 6 of Theme C.

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Multi-threaded applications SE SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions.
Concurrency 101 Shared state. Part 1: General Concepts 2.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?
Multithreading The objectives of this chapter are:
COMPSCI 230 S2C 2015 Software Design and Construction Thread usage and synchronisation Lecture 3 of Theme C C31.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Software Engineering Oct-01 #11: All About Threads Phil Gross.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
© 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.
Fundamentals of Python: From First Programs Through Data Structures
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
50.003: Elements of Software Construction Week 5 Basics of Threads.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
Java Programming: Advanced Topics
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
111 © 2002, Cisco Systems, Inc. All rights reserved.
1 Web Based Programming Section 8 James King 12 August 2003.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
COMPSCI 230 S2C 2015 Software Design and Construction Synchronization (cont.) Lecture 4 of Theme C.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
CSC Multiprocessor Programming, Spring, 2011 Chapter 9 – GUI Applications Dr. Dale E. Parson, week 11.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
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.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Java Thread Programming
Multithreading / Concurrency
Multi Threading.
Multithreaded Programming in Java
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Lecture 21 Concurrency Introduction
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Condition Variables and Producer/Consumer
Multithreading.
Condition Variables and Producer/Consumer
Multithreading.
Multithreaded Programming
CSCI1600: Embedded and Real Time Software
Threads in Java James Brucker.
NETWORK PROGRAMMING CNET 441
CS333 Intro to Operating Systems
Threads and Multithreading
CSCI1600: Embedded and Real Time Software
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Synchronization and liveness
Presentation transcript:

COMPSCI 230 S2C 2013 Software Design and Construction Deadlock, Performance, Programming Guidelines Lecture 6 of Theme C

Learning Goals for Today  Learn a little more Java:  wait(), notify(), notifyAll().  I do not expect you to be able to write code which invokes these methods appropriately.  The syntax is uncomplicated, but the code-design issues are very difficult.  You may be examined on  Your understanding of the ways in which threads can safely signal each other, without “stepping on” each others’ variables.  Your analysis of a multithreaded code, to determine whether or not there is some inappropriate interaction between its thread which may lead to deadlock or to corrupted computations. C62

wait(), notify(), and notifyAll()  Goetz: “In addition to using polling,  which can consume substantial CPU resources and has imprecise timing characteristics,  the Object class includes several methods for threads to signal events from one thread to another.”  Note: Goetz used polling in his TimerTask example.  Let’s review that example now.  Polling is a very important design pattern! It is appropriate  whenever event-signalling isn’t feasible, or  when the resource and time costs of polling are affordable, for example when the polling loop won’t run for very long. C63

Polling a Completion Flag (Goetz1, p. 9-11) // CalculatePrimes -- calculate as many primes as we can // in ten seconds public class CalculatePrimes extends Thread { public static final int MAX_PRIMES = ; public static final int TEN_SECONDS = 10000; public volatile boolean finished = false; public void run() { int[] primes = new int[MAX_PRIMES]; int count = 0; for( int i=2; count<MAX_PRIMES; i++ ) { // a polling loop // Check to see if the timer has expired if (finished) { break; // this thread stops looking for primes } // test i for primality... } C64

Polling example (cont.) public static void main( String[] args ) { Timer timer = new Timer(); final CalculatePrimes calculator = new CalculatePrimes(); calculator.start(); timer.schedule( new TimerTask() { public void run() { calculator.finished = true; } }, TEN_SECONDS ); } } // end of CalculatePrimes C65

Responsiveness vs. efficiency in polling  In CalculatePrimes, the finished flag is polled once for each integer i that is tested for primality. My evaluation:  This is a time-efficient design – the workers will spend most of their time testing for primality, with very little polling overhead.  This is a responsive design for smallish primes – a worker will execute at most a million instructions when testing a 5-digit prime number for primality, so it should “notice” the flag within a few milliseconds.  If better responsiveness is required, the flag should be polled more frequently – making the polling less time-efficient…  Note that you must know a lot about the execution environment, in order to make a good tradeoff of accuracy for efficiency in polled code.  Ideally, the polling overhead is a few percent of total runtime. This optimises responsiveness without noticeably affecting runtime.  “Keep it simple!” Polling is often an appropriate choice, even though it’s not as elegant, efficient, or responsive as a more complex method. C66

Goetz’s Prime-testing Task – my analysis public void run() { int[] primes = new int[ MAX_PRIMES ]; int count = 0; for ( int i=2; count<MAX_PRIMES; i++ ) { if ( finished ) { break; } // poll boolean prime = true; for ( int j=0; j<count; j++ ) { // test for primality if ( i % primes[j] == 0 ) { prime = false; break; } // There are 78,498 primes less than MAX_PRIMES (= ), // so the primality test should complete within a few msec. if ( prime ) { primes[ count++ ] = i; System.out.println( “Found prime: " + i ); } } } C6

Overhead of polling Goetz’s flag  It takes only a few CPU instructions to test a flag if (finished) { break; }  Usual case: there is no extra delay on reading a volatile flag, when the thread already has read-privileges for that flag.  Occasionally: the thread doesn’t yet have read-privileges, and must wait for a main-memory read (maybe a few microseconds).  Worst case: the worker thread must wait for the main() thread to finish its write.  This case is extremely rare, because Goetz’s finished flag is written only once per program execution.  My estimate: Goetz’s workers spend  a few microseconds on each poll, and  a few milliseconds on each primality test when MAX_PRIMES =  The code is probably bottlenecked on println() ! C68

wait(), notify(), and notifyAll()  Goetz1: “ wait() causes the calling thread to sleep until  it is interrupted with Thread.interrupt(),  the specified timeout elapses, or  another thread wakes it up with notify() or notifyAll().  When notify() is invoked on an object,  if there are any threads waiting on that object via wait(), then one thread will be awakened.  When notifyAll() is invoked on an object, all threads waiting on that object will be awakened.  The Object class defines the methods wait(), notify(), and notifyAll().  To execute any of these methods, you must be holding the lock for the associated object.”  For the CompSci 230 exam:  you should know that these methods exist, but their details are not examinable! C69

Usage Notes (Goetz)  “These methods are the building blocks of more sophisticated locking, queuing, and concurrency code.  However, the use of notify() and notifyAll() is complicated.  In particular, using notify() instead of notifyAll() is risky.  Use notifyAll() unless you really know what you're doing.  Rather than use wait() and notify() to write your own schedulers, thread pools, queues, and locks, you should  use the util.concurrent package (see Resources),  a widely used open source toolkit full of useful concurrency utilities. C610

Thread priorities  Goetz: “The Thread API allows you to associate an execution priority with each thread.  However, how these are mapped to the underlying operating system scheduler is implementation-dependent.  In some implementations, multiple – or even all – priorities may be mapped to the same underlying operating system priority.  Many people are tempted to tinker with thread priorities when they encounter a problem like deadlock, starvation, or other undesired scheduling characteristics.  More often than not, however, this just moves the problem somewhere else.  Most programs should simply avoid changing thread priority.” C611

Goetz’s warning about thread-safety  While the thread API is simple, writing thread-safe programs is not.  When variables are shared across threads,  you must take great care to  ensure that you have properly synchronized both read and write access to them.  When writing a variable that may next be read by another thread, or reading a variable that may have been written by another thread,  you must use synchronization to ensure that changes to data are visible across threads. C612

Goetz’ final warning  When using synchronization to protect shared variables,  you must ensure that  not only are you using synchronization, but [also that]  the reader and writer are synchronizing on the same monitor.  Furthermore,  if you rely on an object’s state remaining the same across multiple operations, or  rely on multiple variables staying consistent with each other (or consistent with their own past values),  you must use synchronization to enforce this.  But simply synchronizing every method in a class does not make it thread safe – it just makes it more prone to deadlock. C613

Goetz’s summary  Every Java program uses threads, whether you know it or not.  If you are using either of the Java UI toolkits (AWT or Swing),  Java Servlets,  RMI, or  JavaServer Pages or  Enterprise JavaBeans technologies,  you may be using threads without realizing it.  There are a number of situations where you might want to explicitly use threads to improve the performance, responsiveness, or organization of your programs. These include:  Making the user interface more responsive when performing long tasks  Exploiting multiprocessor systems to handle multiple tasks in parallel  Simplifying modeling of simulations or agent-based systems  Performing asynchronous or background processing C614

Learning Goals for Today  Learn a little more Java:  wait(), notify(), notifyAll().  I do not expect you to be able to write code which invokes these methods appropriately.  The syntax is uncomplicated, but the code-design issues are very difficult.  You may be examined on  Your understanding of the ways in which threads can safely signal each other, without “stepping on” each others’ variables.  Your analysis of a multithreaded code, to determine whether or not there is some inappropriate interaction between its thread which may lead to deadlock or to corrupted computations. C615