Distributed and Parallel Processing George Wells.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

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.
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Chapter 9 (Horstmann’s Book) Multithreading Hwajung Lee.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
CS 11 java track: lecture 7 This week: Web tutorial:
Threading Part 4 CS221 – 4/27/09. The Final Date: 5/7 Time: 6pm Duration: 1hr 50mins Location: EPS 103 Bring: 1 sheet of paper, filled both sides with.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ThreadThread Thread Basics Thread Synchronization Animations.
Definitions Process – An executing program
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Internet Software Development More stuff on Threads Paul Krause.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
Internet Software Development Controlling Threads Paul J Krause.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Java Thread and Memory Model
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
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.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
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.
Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.
1 Chapter 19 Multithreading. 2 Objectives F To understand the concept of multithreading and apply it to develop animation (§19.2). F To develop thread.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
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.
Distributed and Parallel Processing George Wells.
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Multi Threading.
COT 4600 Operating Systems Fall 2009
Background on the need for Synchronization
Multithreading Chapter 9.
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Multithreading Chapter 23.
Synchronization Lecture 23 – Fall 2017.
Condition Variables and Producer/Consumer
Multithreading.
Condition Variables and Producer/Consumer
Semaphore Originally called P() and V() wait (S) { while S <= 0
Java Based Techhnology
Multithreading.
Multithreaded Programming
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
Computer Science 2 06A-Java Multithreading
Threads and Multithreading
Software Engineering and Architecture
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Distributed and Parallel Processing George Wells

Threads in Java Simplified lifecycle New Runnable Running Dea d Blocked Blocking event Unblocked Scheduler run() completes start() Thread scheduling is preemptive, but not necessarily time-sliced – Use Thread.sleep(x) or Thread.yield()

Thread Control Various methods: – isAlive() – getpriority() and setpriority(x) – join() – Thread.currentThread()

Exercise Write a class ( MyThread ) – Implements Runnable – run() consists of a loop (10x) printing the thread's name and sleeping for 1s Write a main program that creates 3 of these threads and runs them – Use join() to wait for them – Experiment with the setName method – Experiment with changing priorities and see what the effects are

Synchronisation Seen the effects of lack of synchronisation – Mandelbrot example Solution: – Use synchronized modifier on method Synchronized methods/blocks obtain a lock on the object before proceeding – No other thread can execute synchronised code on that object while the lock is held – Mutual exclusion (mutex) More to it than that!

Example: Producer-Consumer Specialised instance of the pipeline pattern – One process/thread produces data – Another uses (consumes) this data Uses some form of “communication channel” – We assume a simple queue

public class ArrayQueue { private T[] data; private int hd, tl; public ArrayQueue () { data = (T[])new Object[10]; hd = tl = -1; } // Constructor public void add (T item) { tl = (tl + 1) % data.length; data[tl] = item; if (hd == -1) // First item in queue hd = tl; } // add public T remove () { int tmpIndex = hd; if (hd == tl) // Was last element hd = tl = -1; else hd = (hd + 1) % data.length; return data[tmpIndex]; } // remove } // class ArrayQueue Queue Class

Main Program Producer class – Generates stream of random numbers – Places numbers in queue Consumer class – Retrieves numbers from queue – Displays square

Problem! No synchronisation But we need more than just synchronized methods – Need to handle queue empty/full conditions wait() – Relinquishes lock, and waits for notification notify() and notifyAll() – Release one/all waiting threads – It/they must wait to reacquire the lock

Queue Class The add method: public synchronized void add (T item) { while (((tl + 1) % data.length) == hd) wait(); // No space tl = (tl + 1) % data.length; data[tl] = item; if (hd == -1) // First item in queue hd = tl; notifyAll(); // Release any consumers } // add A little more complicated – wait() may throw InterruptedException

Queue Class The remove method: public synchronized T remove () { while (hd == -1) // No data wait(); int tmpIndex = hd; if (hd == tl) // Was last element hd = tl = -1; else hd = (hd + 1) % data.length; notifyAll(); // Release waiting producers return data[tmpIndex]; } // remove

Exercise Experiment with slowing either the producer or the consumer down – Use Thread.sleep() Experiment with multiple producers and/or consumers

Threads in Java Full lifecycle New Runnable Running Dea d Blocked Blocking event Unblocked Scheduler run() completes start() Blocked in lock pool Blocked in wait pool synchronized wait() notify() or interrupt() Acquires lock

Deadlock Major problem in parallel systems Thread 1 obtains lock on object A Attempts to obtain lock on object B Thread 2 obtains lock on object B Attempts to obtain lock on object A Hard to detect and prevent – Enforce rigid ordering of obtaining multiple locks – suspend, resume and stop methods deprecated