Two Phase Termination 模式

Two Phase Termination 模式表示先执行完终止处理,再终止线程的模式。

模式特点

  • 安全的终止线程

  • 必定会进行线程终止

  • 发出请求后尽快响应终止处理

实际案例

类信息概览:

类名 说明
Main.java 方法的总入口
CounterUpThread.java 进行计数的线程类

定义

  • CounterUpThread.java
package com.github.houbb.thread.learn.easy.learn.twoPhaseTerminal;

/**
 * 2018/2/3
 *
 * @author houbinbin
 * @version 1.0
 * @since 1.7
 */
public class CounterUpThread extends Thread {

    private long counter = 0;

    private volatile boolean isShutdownRequest = false;

    /**
     * 终止请求
     * @see Thread#interrupt() 打断当前的 sleep() and wait(); 提高响应速度
     */
    public void shutdownRequest() {
        isShutdownRequest = true;
        interrupt();
    }

    /**
     * 状态
     * @return
     */
    public boolean getIsShutdownRequest() {
        return isShutdownRequest;
    }

    @Override
    public void run() {

        try {
            while(!getIsShutdownRequest()) {
                doWork();
            }
        } catch (InterruptedException e) {
            //ignore
        } finally {
            doShutDown();
        }
    }

    private void doWork() throws InterruptedException {
        counter++;
        System.out.println("doWork counter = " + counter);
        Thread.sleep(1000);
    }

    private void doShutDown() {
        isShutdownRequest = false;
        System.out.println("doShutDown counter = " + counter);
    }
}

测试

  • Main.java
package com.github.houbb.thread.learn.easy.learn.twoPhaseTerminal;

import com.github.houbb.thread.learn.easy.learn.ThreadUtil;

/**
 * 2018/2/3
 *
 * @author houbinbin
 * @version 1.0
 * @since 1.7
 */
public class Main {

    public static void main(String[] args) throws InterruptedException {
        System.out.println("Main BEGIN...");
        CounterUpThread counterUpThread = new CounterUpThread();
        System.out.println("counterUpThread START...");
        counterUpThread.start();

        ThreadUtil.sleep(1000);

        System.out.println("counterUpThread shutdown...");
        counterUpThread.shutdownRequest();

        System.out.println("Main JOIN...");
        counterUpThread.join(); //等待线程终止

        System.out.println("Main END...");
    }
}

  • 测试结果
Main BEGIN...
counterUpThread START...
doWork counter = 1
counterUpThread shutdown...
doWork counter = 2
Main JOIN...
doShutDown counter = 2
Main END...

Process finished with exit code 0

实现方式

UML & Code

UML

Two Phase Termination

Code

代码地址

Two Phase Termination

系列导航

多线程系列导航