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

javathread.jpeg

Balking pattern

** balk ** means to stop and go home. Baseball balk is also balk. Similar to the Guarded Suspension pattern, the Balking pattern also has ** guard conditions **. In the Balking pattern, if the guard condition is not met, it will be interrupted immediately. This is different from the Guarded Suspension pattern, which waits until it is ready to run.

Think of something like the auto-save feature of an editor. There is a thread that periodically writes the contents of the current data to a file. Save only when the data content is updated. The guard condition is that there is a difference in the data contents, and if there is no difference, it returns (balk) without writing to the file.

(See this manual for the entire code)

...
    public synchronized void change(String newContent) { 
        content = newContent; 
        changed = true; 
    } 
 
    public synchronized void save() throws IOException { 
        if (!changed) { //If not changed
            return; //balk
        } 
        doSave(); 
        changed = false; 
    }
...

Character

The GuardedObject role is a class that has a guarded method. If the guard condition is met, it will be executed immediately, and if it is not met, it will return without going to the actual processing. The GuardedObjec role may have a method (stateChangingMethod) that changes the state of the instance. In the sample program, the Data class plays this role, the save method corresponds to guardedMethod, and the change field corresponds to stateChangingMethod.

When to use

--When there is no need for processing --If you don't want to wait for the guard conditions to be met --If the guard condition is met only the first time. For example, if you want to check if it is initialized, and if it is not initialized, initialize it only once.

...
    public synchronized void init() { 
        if (initialized) { 
            return; 
        } 
        doInit(); 
        initialized = true; 
    }

A "variable whose state changes only once", such as an initialized field, is generally called a ** latch ** (latch).

time out

As an intermediate between the Balking pattern and the Guarded Suspension pattern, there is a workaround that ** waits for a certain period of time until the guard condition is met **. The process of waiting for a certain period of time until the guard condition is satisfied and then balking when the condition is not satisfied is called ** guarded timed ** or ** timeout **.

    obj.wait(1000) //Wait 1000ms

However, Java has no way to distinguish between ** notify / notifyAll and timed out **.


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)

Recommended Posts

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 11)
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)
Java Design Patterns
What I learned in Java (Part 2) What are variables?
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
[Java] Basic summary of Java not covered by Progate ~ Part 2 ยท List ~
Road to Java Engineer Part2 What kind of language is Java?
Read design patterns in Ruby
[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
What I have learned in Java (Part 1) Java development flow and overview
Summary of ORM "uroboroSQL" that can be used in enterprise Java
[For beginners] Summary of java constructor
Summary of [Java silver study] package
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
Creating lexical analysis in Java 8 (Part 2)
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)