Internet Software Development Controlling Threads Paul J Krause.

Slides:



Advertisements
Similar presentations
Operating Systems Part III: Process Management (Process Synchronization)
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Chapter 6: Process Synchronization
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CS Concurrent Programming CS 3331 Fall 2009.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: Which.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
Object-Oriented Software Engineering Concurrent Programming.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
6/16/2015 Chapter Eight Process Synchronisation. Index Objectives Concurrent processes and Asynchronous concurrent processes Process synchronisation Mutual.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Internet Software Development More stuff on Threads Paul Krause.
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.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
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.
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
1 Ivan Marsic Rutgers University LECTURE 19: Concurrent Programming.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
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.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Tutorial 2: Homework 1 and Project 1
Principles of Software Development
Multithreading / Concurrency
Background on the need for Synchronization
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Module 7a: Classic Synchronization
Synchronization and Semaphores
Multithreading.
Lecture 2 Part 2 Process Synchronization
Dr. Mustafa Cem Kasapbaşı
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
Computer Science 2 06A-Java Multithreading
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
Chapter 6: Synchronization Tools
Threads and Multithreading
Software Engineering and Architecture
Presentation transcript:

Internet Software Development Controlling Threads Paul J Krause

Contents  Quick recap  Thread Safety  Synchronization  Locks  Guarded suspension

Threads vs. Processes  A thread is a single flow of control within a program  It performs one of the tasks that a program needs to perform to achieve its goals  If a program needs to perform several tasks, these could be handled in different threads (and hence performed concurrently)  These threads will interact and cooperate through a shared memory space

The Risks of Threads  Different threads can interact via shared variables and objects  The execution of each thread may proceed independently of the others  In general, the relative ordering of execution of the different threads is non- deterministic  This can lead to safety and liveness problems

Thread Safety  Properties that we require to hold throughout the lifetime of a program  Essentially saying “Nothing bad should happen”  Bad Things typically can happen if a thread is interrupted whilst in the process of modifying an object

Bank Account Class public class Account { private long balance; // … public boolean withdraw(long amount) { if (amount <= balance) { balance = balance - amount; return true; } else { return false; } }}

This could happen! BalanceWithdraw1Withdraw2 1,000,000 amount <= balance 1,000,000 0 balance = …; -1,000,000 -1,000,000 return true; -1,000,000

Synchronisation  This was an example of a race condition  To be thread-safe, a class must ensure: consistency of the states of its instances, and consistency of the states of its instances, and consistency of the results of method invocations in the presence of multiple threads consistency of the results of method invocations in the presence of multiple threads  We have seen the Account class is not thread- safe  We can use Synchronisation to make it thread- safe

Synchronisation II  What was the problem with the Account class?  Answer -  More than one thread at a time was able to enter a “critical region” in the code  We need to: identify sections of code that should only be accessed by one thread at a time identify sections of code that should only be accessed by one thread at a time use the Java synchronisation mechanism to ensure a second thread cannot access a critical section use the Java synchronisation mechanism to ensure a second thread cannot access a critical section

Making operations atomic  An atomic operation is one that cannot be interrupted  Reading and assignment of variables of primitive types is atomic except long and double except long and double  For other operations, use synchronized to make them atomic

Synchronising methods class MyClass { synchronized void aMethod() { // do some stuff … }} Here, the whole method is in the critical region

Synchronising statements class MyClass { void aMethod() { // do some stuff … synchronised(this) { // atomic stuff … }}} “this” could be replaced by some other reference type

What to synchronize? public class Account { private long balance; // … public boolean withdraw(long amount) { if (amount <= balance) { balance = balance - amount; return true; } else { return false; } }}

Locks  Each object has an associated lock  A thread must obtain exclusive possession of the appropriate lock before it can enter a critical region i.e. the region marked by a synchronized statement i.e. the region marked by a synchronized statement  The lock is released when the thread leaves the critical region

Which lock?  For a synchronized instance method, the lock belongs to “this” instance  For a synchronized statement: synchronised(exp) { // atomic stuff … }  the lock associated with exp is used

Sharing locks public class A { synchronized void m1() { … } synchronized void m2() { … } void m3() { … } }

Is that sufficient? Thread ProducerConsumer SyncBoundedQueue

Cooperating threads  Can resolve additional problems with the use of guarded suspension  The guard is tested before a method is executed  If the guard is true, execution of the method will continue  If the guard is false, execution will be suspended until it becomes true

Thread control  Guarded suspension can be implemented through the use of the following thread control methods:  wait(): This is invoked if a thread is temporarily unable to continue (the guard is false)  notify(): One thread notifies another thread that it may continue  notifyAll(): All threads in the wait queue associated with the receiving object will be notified

Summary  Multiple threads may lead to safety issues  We have seen how synchronisation can be used to make programs “thread-safe”  But that still does not mean that multi- threaded programs will be perfectly behaved  We have introduced guarded suspension to solve additional problems