Two Phase Termination模式

1.线程停止的two phase termination

线程分为两个阶段,第一个阶段是正常工作,第二个阶段是处理终止信息如资源释放。

第二个阶段要确保如下几点:

    1.保证安全性

    2.百分之百确保线程结束

    3.对资源的释放时间要确保在一个可控范围内

例如:

public void run(){

    try{

        this.chat();

    }catch(Exception e){

        e.printStackTrace();

    }finally{

        this.release();

    }

}



public void release(){

    try{

        if(socket != null){

            socket.close();

        }

    }catch(Throwable e){



    }

}

2.线程终止的two phase termination

    通过为进程注入一个或者多个hook实现

发布了115 篇原创文章 · 获赞 57 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/qq_35211818/article/details/104186595
two