Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Inter-Thread communication State dependency: Guarded Methods.

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

CMSC 330: Organization of Programming Languages Threads Classic Concurrency Problems.
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.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
1 Chapter 8 Three Interfaces: Cloneable, Serializable, and Runnable.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
ThreadThread Thread Basics Thread Synchronization Animations.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
More Multithreaded Programming in Java David Meredith Aalborg University.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Object Oriented Programming
Internet Software Development More stuff on Threads Paul Krause.
Java Programming: Advanced Topics
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
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.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
Optimistic Design 1. Guarded Methods Do something based on the fact that one or more objects have particular states  Make a set of purchases assuming.
Internet Software Development Controlling Threads Paul J Krause.
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 in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
What is a ‘ thread ’ ? Free Online Dictionary of Computing (FOLDOC) Sharing a single CPU between multiple tasks (or "threads") in a way designed to minimize.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
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.
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.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 More on Thread API.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
CP — Concurrent Programming 6. Liveness and Guarded Methods Prof. O. Nierstrasz Wintersemester 2005 / 2006.
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.
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.
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.
© Andy Wellings, 2004 Thread Priorities I  Although priorities can be given to Java threads, they are only used as a guide to the underlying scheduler.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
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.
Java Thread Programming
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Multithreading Chapter 9.
More About Threads.
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Threads II IS
Condition Variables and Producer/Consumer
Multithreading.
Cancellation.
Condition Variables and Producer/Consumer
Multithreading.
Multithreaded Programming
Java Concurrency 17-Jan-19.
Component-Based Software Engineering
Lecture 9 Synchronization.
Java Concurrency.
Java Concurrency.
Concurrent programming
Java Concurrency 29-May-19.
Software Engineering and Architecture
Presentation transcript:

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Inter-Thread communication State dependency: Guarded Methods

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 2 Monitors Through synchronization one thread can safely change values that another thread will read; how does the second thread know that the values have changed? Check-and-act methods are methods designed to refuse to perform actions unless they can ensure actions will succeed, in part by checking their pre-conditions

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 3 Policies on failed pre-conditions Balking: throw an exception indicating refusal, not failure. Guarded suspension: suspend action until the pre-condition becomes true Time-outs: place a bound on the suspension.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 4 Monitor mechanics Guarded methods are usually built using the following methods: –Object.wait() –Object.notify() –Object.notifyAll()

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 5 Strategies For each condition that needs to be waited on, write a guarded wait loop that causes the current thread to block if guard is false. Ensure that every method causing state changes that affect the truth value of any waited-for condition notifies threads waiting for state changes, causing them to wake up and recheck their guard conditions A member variable is checked by waiting thread, and modified by thread doing the modification. The checking and modification occur inside synchronized methods to avoid race conditions.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 6 Every object has a wait set manipulated by wait, notify, notifyAll, thread.interrupt() Objects containing locks and wait sets are called monitors Any Object (read class) can serve as a monitor Because of the relationship between wait sets and locks, the methods wait, notify, notifyAll may be invoked only when the synchronization lock is held on their targets, a run- time error will be thrown otherwise

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 7 wait () A wait invocation results in the following actions: –If current thread has been interrupted, method exists immediately throwing InterruptedException ; otherwise the current thread is blocked. –JVM places the thread in the internal and inaccessible wait set associated with the target object. –Synchronization lock for target object is released, but all other locks held by thread are retained. –Full release is obtained even if lock is re-entrantly held due to nested synchronized calls on target object. Upon resumption the lock status is fully restored. –When wait pauses the thread, it atomically releases the lock on the object.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 8 notify(), notifyAll() An arbitrary chose thread T, if exists is removed by the JVM from the internal wait set associated with target object. No guarantee on the threat T selected. T must re-obtain the synchronization lock for the target object, which will always cause it to block at least until the thread calling notify releases the lock. It will continue to block if some other thread obtains lock first. T is resumed from the point of its wait. NotifyAll works essentially the same as notify, except that the steps occur, in effect simultaneously, for all threads in the wait set of the target object. However because they must acquire the lock, threads continue one at a time. Fundamental difference between notify() and notifyAll() is that if more than one thread is waiting notification, notify() will only signal one of waiting threads, while notifyAll() signals all waiting threads.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 9 Notify as an optimization Using notify is an optimization over notifyAll when –All threads are waiting for same condition –At most one thread can benefit for the condition being met. –This is contractually true for all subclasses. Otherwise use notifyAll.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 10 Missed notification Thread B signals threadA, but threadA is not yet waiting on the notification.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 11 Early notification If a thread is notified wile waiting, but the condition the thread is waiting for has not been met, thread received an early notification. An early notification also occurs if condition is briefly met but quickly changes so is no longer met. This is due to subtle error in code: use a while for waiting not an if. An example: two threads are waiting for an element to be added, and only one item is added.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 12 Canceling a thread There are occasions to cancel a running thread (user presses cancel button) Cancellation is requested by interrupting the thread and writing the thread such that it watches for and responds to being interrupted Thread 1 thread 2 thread2.interrupt();while(!interrupted()) doSomeWork(); An interrupt does not force the thread to halt, but it will interrupt sleeping or waiting threads.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 13 Interrupts If thread.interrupt() is invoked for a thread suspended in a wait, same notify mechanism applies, except that after re-acquiring the lock, wait method throws an InterruptedException and thread’s interrupt status is set to false. NO telling behavior if an interrupt and a notify happen simultaneously. isInterrupted () tests whether a thread has been interrupted. interrupted (), a static method that tests whether the current thread has been interrupted, and then clears the “interrupted” state of the thread. It returns true if the thread had been interrupted; false otherwise. Interrupted state of a thread can only be clear by that thread. There is no way for one thread to “un-interrupt” another thread. One of the actions of an interrupted thread is to clean up before responding to the interrupt. Methods sleep(), and wait() will throw InterruptedException; this exception will clear the interrupted state when thrown.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 14 Blocking and interrupt() Any method performing a blocking operation should be designed to be cancelled with interrupt () and should throw an appropriate exception if that occurs. –Example: sleep (), and wait () In some systems, blocking I/O will respond to interruption by throwing InterruptedException Even if the interruption cannot be responded to during I/O, systems may check for interruption at start of operation and throw the exception Hence the need for an interrupted thread to clear its interrupted state if needs to perform I/O as part of its cleanup. You cannot assume that interrupt () will unblock a thread that is performing I/O. Golden rule: never hide an interrupt by clearing explicitely or by catching an InterruptedException and continuing normally.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 15 Threads and object state A thread dies when –The run method returns normally. –The run method completes abruptly. –The destroy method is invoked on the thread. When a thread dies the thread object does not go away, so you can still access its state. Useful when using the join () method.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 16 Time waits: wait(long msecs), wait(long msecs, int nanosecs) Operate as the untimed version, except that when time out occurs, it is released automatically without need for notification. No status indication differentiating waits that return via notifications versus a time-out. Counterintuitively, wait(0), and wait(0,0) are equivalent to wait() A timed wait may resume an arbitrarily amout of time after the requested bound due to thread contention, scheduling policies, and timer granularities.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 17 Class X { public synchronized void w() throws InterruptedException{ before(); wait(); after(); } public synchronized void n(){ notifyAll();} public void before(); public void after(); } begin x.w() acquire lock before(); wait: release lock enter wait set begin x.w() acquire lock before(); wait: release lock enter wait set begin x.n() Wait for lock acquire lock notifyAll() release lock Exit wait set Wait for lock Acquire lock After(); Release lock Exit wait set Wait for lock Acquire lock After(); Release lock T1 T2 T1T2T3 x wait set

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 18 Class X { public synchronized void w() throws InterruptedException{ before(); wait(); after(); } public synchronized void n(){ notifyAll();} public void before(); public void after(); } begin x.w() acquire lock before(); wait: release lock enter wait set begin x.w() acquire lock before(); wait: release lock enter wait set begin x.n() Wait for lock acquire lock notifyAll() release lock Exit wait set Wait for lock Acquire lock After(); Release lock Exit wait set Wait for lock Acquire lock After(); Release lock T1 T2 T1T2T3 x wait set

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 19 Cubby Hole example And Queue example (ch8, ch18)

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 20 join API void join() Waits for this thread to die. void join(long millis) Waits at most millis milliseconds for this thread to die. void join(long millis, int nanos) Waits at most millis milliseconds plus nanos nanoseconds for this thread to die. In general, join is a rather crude method for thread synchronization, but is need is wanted in some instances.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 21 Ending application execution Your application has at least one thread (the main()) created when application is invoked. All other threads are created by the main() thread or threads created by main. Two kinds of threads: user and daemon. An application terminates when all user threads terminate. All daemon threads are destroyed by JVM when it detects that all user threads are finished.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 22 Thread management and security: Thread group Threads are organized into groups for management or security Thread groups form a hierarchy starting with its top group or system thread group. A thread group can be managed as a unit: –Interrupting all threads in a group at once. –Placing a limit on max priority of threads in group. –Used to define a security domain.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 23 Threads and exception An uncaught exception in a thread will be handled –By the thread’s parent group if there is one. –By invoking the exception’s printStackTrace method is there is no parent group, so that information about the exception is displayed. –But recall, this print appears in a terminal screen. If app does not have a terminal screen the error will go unnoticed.!!!!!

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 24 ThreadLocal This class provides ThreadLocal variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. ThreadLocal objects are typically private static variables in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID). Each thread holds an implicit reference to its copy of a ThreadLocal as long as the thread is alive and the ThreadLocal object is accessible; after a thread goes away, all of its copies of ThreadLocal variables are subject to garbage collection (unless other references to these copies exist).