Chapter 7 Cancellation and Shutdown Alfred Theorin, Ph.D. student - - PowerPoint PPT Presentation

chapter 7
SMART_READER_LITE
LIVE PREVIEW

Chapter 7 Cancellation and Shutdown Alfred Theorin, Ph.D. student - - PowerPoint PPT Presentation

Chapter 7 Cancellation and Shutdown Alfred Theorin, Ph.D. student Department of Automatic Control Lund University Outline Cancellation Why and when it is needed How it is accomplished Abnormal thread termination JVM shutdown


slide-1
SLIDE 1

Chapter 7 Cancellation and Shutdown

Alfred Theorin, Ph.D. student

Department of Automatic Control Lund University

slide-2
SLIDE 2

Outline

  • Cancellation
  • Why and when it is needed
  • How it is accomplished
  • Abnormal thread termination
  • JVM shutdown
slide-3
SLIDE 3

Cancellation: Why or When?

  • User-requested
  • Time-limited task
  • Application event
  • Error
  • Shutdown
slide-4
SLIDE 4

Cancellation: How?

  • No safe way to just stop a thread
  • Cooperative mechanism
  • Cancellation policy
  • How to request cancellation
  • What the task does in response
  • Interruption: interrupted status in Thread

Interruption is usually the most sensible way to implement cancellation.

slide-5
SLIDE 5

Interruption

public class Thread { // Request interruption, set interrupted status public void interrupt() { ... } // Get interrupted status public boolean isInterrupted() { ... } // Clear interrupted status public static boolean interrupted() { ... } ... }

slide-6
SLIDE 6

Interruption Example

public class MyThread extends Thread { public void run() { while (!isInterrupted()) { doWork(); } } ... } MyThread t = new MyThread(); ... t.interrupt();

slide-7
SLIDE 7

Blocking Operations

public class MyThread extends Thread { public void run() { while (!isInterrupted()) { doWork(); sleepOneHour(); } } ... }

Not responsive to interruption. Don't do this.

slide-8
SLIDE 8

Interruptable Blocking Operations

Many blocking operations are interruptable. They terminate prematurely on interruption by clearing the interrupted status and throwing an InterruptedException

slide-9
SLIDE 9

Interruptable Blocking Operations

public class MyThread extends Thread { public void run() { while (!isInterrupted()) { doWork(); try { sleep(3600000); } catch (InterruptedException e) { interrupt(); } } } ... }

slide-10
SLIDE 10

General Purpose Code

General purpose code should never swallow interruption requests.

public void sleepOneHour() { try { sleep(3600000); } catch (InterruptedException e) { } }

Interruption request is lost. Don't do this.

slide-11
SLIDE 11

General Purpose Code

public void sleepOneHour() throws InterruptedException { sleep(3600000); }

General purpose code should never swallow interruption requests.

slide-12
SLIDE 12

Non-interruptable Blocking

  • synchronized: Cannot be interrupted
  • Non-interruptable blocking operations
slide-13
SLIDE 13

Non-interruptable Blocking Operations

public class SocketThread extends Thread { private Socket socket; public SocketThread(Socket socket) { this.socket = socket; } public void run() { ... try { int c = socket.getInputStream().read(); } catch (IOException e) { } ... } }

Not responsive to interruption requests. Don't do this.

slide-14
SLIDE 14

Encapsulate Interruption

public class SocketThread extends Thread { ... public void interrupt() { try { socket.close(); } catch (IOException ignored) { } finally { super.interrupt(); } } }

slide-15
SLIDE 15

Abnormal Thread Termination

public interface UncaughtExceptionHandler { void uncaughtException(Thread t, Throwable e); } Thread.setDefaultUncaughtExceptionHandler(...);

  • Uncaught Exceptions in threads
  • Default behavior: Print to System.err
slide-16
SLIDE 16

JVM Shutdown

Orderly shutdown

  • Last nondaemon thread terminates
  • Call to System.exit()
  • Ctrl-C
  • ...

Abrupt shutdown

  • Call to Runtime.halt()
  • Killed by OS
slide-17
SLIDE 17

Shutdown Hooks

  • Executed for orderly shutdowns
  • Last chance for cancellation and cleanup
  • Registered with

Runtime.addShutdownHook(...)

  • Executed concurrently
  • Only use one if order matters
  • Should be fast
slide-18
SLIDE 18

Summary

  • Cancellation – Interruption
  • Interrupted status
  • Blocking operations: Interruptable
  • Blocking operations: Non-interruptable
  • Encapsulate interruption
  • Abnormal thread termination
  • JVM shutdown