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

javathread.jpeg

Thread-Per-Message pattern

A new thread is assigned for each instruction or request, and that thread performs processing. This is the Thread-Per-Message pattern.

Consider the following example. The Main class requests the Host class to display characters. The Host class creates and starts a thread that processes the request. The launched thread uses the Helper class to do the actual display.

(See this manual for the entire code)

Host.java


public class Host { 
    private final Helper helper = new Helper(); 
    public void request(final int count, final char c) { 
        new Thread() { 
            public void run() { 
                helper.handle(count, c); 
            } 
        }.start(); 
    } 
}

Character

Client role The Client role issues a request to the Host role. The Client role does not know how the Host role fulfills the request. In the sample program, the Main class played this role.

Host role When the Host role receives a request from the Client role, it creates a new thread and starts it. The newly created thread uses the Helper role to process the request. In the sample program, the Host class played this role.

Helper role The Helper role provides the Host role with the function of processing the request. A new thread created by the Host role uses the Helper role. In the sample program, the Helper class played this role.

Tips for expanding your thoughts

** The order in which the handle method is processed in the Thread-Per-Message pattern is not always the same as the order in which the request method is called. Therefore, it is inappropriate to use this pattern when the order of processing is meaningful. ** **

** In the Thread-Per-Message pattern, the request method side does not wait for the handle method to complete. Therefore, the execution result of handle cannot be obtained on the request side. Therefore, this pattern is used when the processing result is not needed. ** If you want the result of processing, use Future pattern.


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 (Multithread 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)

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 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
[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
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)