Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 12)

javathread.jpeg

Two-Phase Termination pattern

The point is --End safely (safety) --Be sure to perform termination processing (survival) --Start the termination process as soon as possible after issuing the termination process (responsiveness)

Consider a program that terminates a thread that counts up at intervals of about 500 milliseconds after about 10 seconds.

(See this manual for the entire code)

Main.java


public class Main { 
    public static void main(String[] args) { 
        try { 
            CountupThread t = new CountupThread(); 
            t.start(); 
            Thread.sleep(10000); 
            t.shutdownRequest(); 
            t.join(); 
        } catch (InterruptedException e) { 
            e.printStackTrace(); 
        } 
    } 
}

CountupThread.java


public class CountupThread extends Thread { 
    private long counter = 0; 
    private volatile boolean shutdownRequested = false; 
 
    public void shutdownRequest() { 
        shutdownRequested = true; 
        interrupt(); 
    } 
 
    ... 

    public final void run() { 
        try { 
            while (!isShutdownRequested()) { 
                doWork(); 
            } 
        } catch (InterruptedException e) { 
        } finally { 
            doShutdown(); 
        } 
    } 
 
    private void doWork() throws InterruptedException { 
        counter++; 
        System.out.println("doWork: counter = " + counter); 
        Thread.sleep(500); 
    } 
 
    ... 
}

Character

Termination Requester role The Terminator Requester role issues a termination request to the Terminator role. In the sample program, the Main class played this role.

Terminator role The Terminator role receives the termination request and actually performs the termination processing. The Terminator role provides a shutdownRequest method that represents a termination request. The Terminator role has a flag (latch) that indicates whether or not it has received a termination request. In the sample program, the CountupThread class played this role.

Tips to broaden your thinking

Do not use the stop method of the Thread class With stop, the thread ends with the exception java.lang.ThreadDeath, even while the critical section is in progress.

Flag testing alone is not enough Is it necessary to call the ** interrupt method ** in the shutdownRequest method? Isn't the shutdownRequest flag alone? With only the shutdownRequested flag, for example, if the thread is sleeping, no matter how much the flag is set to true, the termination process will not start. It may start the termination process after the sleep time, but that makes it less responsive. You can use the interrupt method to interrupt sleep and wait.

Interrupt state testing alone is not enough This time, on the contrary, is it not enough to just call the interrupt method? For example, if you write the following in the doWork method that the thread is executing, it will not work.

    private void doWork() throws InterruptedException { 
        counter++; 
        System.out.println("doWork: counter = " + counter);
        try { 
            Thread.sleep(500);
        } catch (InterruptedException e) {
        } 
    }

About the interrupt method ** You might think that when you call the interrupt method, the target thread always throws an InterruptedException, which is a misunderstanding. A method called interrupt only changes the thread's interrupt state. The interrupt state is a state that indicates whether or not a thread is interrupted. sleep, wait, and join throw InterruptedExcpetion because they check the thread's interrupted state inside the method and explicitly throw an InterruptedException. Once an InterruptedException is thrown with wait, sleep, join, the thread is no longer in an interrupted state. ** **


Relation Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 1) Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 2) Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 3) Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 4) Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 5) Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 6) Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 7) Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 8) Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 9) Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 10) Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 11)

Recommended Posts

Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 10)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 7)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 3)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 9)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 6)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 4)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 5)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 2)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 1)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 12)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 8)
[Java] Summary of design patterns
A quick review of Java learned in class part4
Summary of Java language basics
A quick review of Java learned in class part3
A quick review of Java learned in class part2
I read Hiroshi Yuki "Introduction to Design Patterns Learned in Java Language" (SB Creative)
A quick review of Java learned in class
Summary of what I learned in Spring Batch
Try design patterns in C language! Memento pattern-Let's memorize the memories of the data
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
What I learned in Java (Part 3) Instruction execution statement
Summary of how to implement default arguments in Java
Summary of Java support 2018
Java design pattern summary
What I learned in Java (Part 4) Conditional branching and repetition
Road to Java Engineer Part2 What kind of language is Java?
Read design patterns in Ruby
[Java11] Stream Summary -Advantages of Stream-
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
[Java] Summary of for statements
Summary of Java Math class
Implementation of gzip in java
[Java] Summary of control syntax
Implementation of tri-tree in Java
Summary of java error processing
[Java] Summary of mathematical operations
[For beginners] Summary of java constructor
Basic usage of java Optional Part 1
thread safe process in java language
AtCoder 400 points algorithm summary (Java edition)
List of members added in Java 9
List of types added in Java 9
Summary of object-oriented programming using Java
Implementation of like function in Java
Creating lexical analysis in Java 8 (Part 1)