Cancellation.

Slides:



Advertisements
Similar presentations
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Advertisements

Concurrency (p2) synchronized (this) { doLecture(part2); } synchronized (this) { doLecture(part2); }
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
CMSC 330: Organization of Programming Languages Threads Classic Concurrency Problems.
Concurrency and Thread Yoshi. Two Ways to Create Thread Extending class Thread – Actually, we need to override the run method in class Thread Implementing.
Thread Control methods The thread class contains the methods for controlling threads Thread() create default thread Thread(target: runnable ) creates new.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Concurrency…leading up to writing a web crawler. Web crawlers.
Software Engineering Oct-01 #11: All About Threads Phil Gross.
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.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
More Multithreaded Programming in Java David Meredith Aalborg University.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Inter-Thread communication State dependency: Guarded Methods.
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.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
Practice Session 8 Blocking queues Producers-Consumers pattern Semaphore Futures and Callables Advanced Thread Synchronization Methods CountDownLatch Thread.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
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.
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.
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.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Chapter11 Concurrent. 集美大学 计算机工程学院 Java 程序设计 年 第二版 Concurrent ●Computer users take it for granted that their systems can do more than one thing.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
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.
Software Design 13.1 From controller to threads l Threads are lightweight processes (what’s a process?)  Threads are part of a single program, share state.
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.
Java Thread Programming
Multithreading The objectives of this chapter are:
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Doing Several Things at Once
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Multi Threading.
Background on the need for Synchronization
Practice Session 8 Lockfree LinkedList Blocking queues
Multithreaded Programming in Java
Multithreading Chapter 9.
Lecture 21 Concurrency Introduction
More About Threads.
Threads Chate Patanothai.
Threads II IS
Chapter 12 Exception Handling and Text IO
Multithreading.
Multithreading.
Multithreaded Programming
Java Concurrency 17-Jan-19.
21 Threads.
Java Concurrency.
CS333 Intro to Operating Systems
Java Concurrency.
Threads and Multithreading
Java Concurrency 29-May-19.
Multithreading The objectives of this chapter are:
CSE 542: Operating Systems
CSE 542: Operating Systems
Chapter 9 Multithreading
More concurrency issues
Presentation transcript:

Cancellation

Why we should terminate our threads cleanly?

NeverEnds (at least) two threads are created: first thread is main thread, executes main()  second thread is t main() thread terminates almost immediately. t never terminates - loops forever program will never terminate process only terminates when all of its threads have terminated Runnable based thread terminates when  run() does

t.stop()? No JAVA interface to shutting down threads from the outside no way to invoke t.stop() from main

t.stop()? assume Java allows one thread to stop another assume while T1 calls T2.stop(), T2 holds some locks and is in the middle of changing the internal state of some object protected by these locks. if locks are not released when T2 is stopped - deadlock in the future if locks are immediately released the object which T2 was working on is in inconsistent state

introduce a new Exception? introduce a new exception – ThreadDeath calls to stop() throws ThreadDeath to the thread, interrupting the thread's current execution. good in theory but impractical and dangerous: ThreadDeath exception can be thrown anywhere! examine all code, take care of cleanup prior to terminating thread ThreadDeath exception can be thrown even while we are cleaning after a previous ThreadDeath exception

How to terminate threads cleanly? use a variable in Thread object that indicates someone requested this thread to stop thread object checks this variable at regular intervals in run() method when it is safe to do so determine whether it accepts to stop (return from run) or not

How to terminate threads cleanly? thread cancellation is a cooperative protocol: one thread asks another one to stop receiving thread can accept or reject request

How to interrupt long waits? Threads may sometime be stopped for a long period of time. Threads may: blocked waiting to enter synchronize block sleep() wait() join() perform IO

interrupted() Sometimes, when we want to tell a thread T that it needs to stop T may not notice our request for some time, since T's execution is currently stopped interrupted status - internal flag to every thread : interrupt() - flag may be set by any other thread Thread.isInterrupted() - flag may be checked and cleared

interrupted flag If Thread T is in If thread, D calls T.interrupt(): Sleep() wait() join() If thread, D  calls T.interrupt(): InterruptedException will be thrown to T break from wait interrupted status of T is cleared

interrupted flag If  T is performing IO, using an interruptible channel: channel will be closed  T's interrupt status will be set ClosedByInterruptException will be thrown to T

interrupted flag If T is waiting for a Selector to return T's interrupt status will be set call to the selector will return.

interrupted flag If none of the previous conditions holds then 's interrupt status will be set.

interrupt mechanism is used in conjunction with the methods mentioned above