为什么四郎的 SubjectCallable 需要调用还原方法?



SubjectCallable的调用方法:

public V call() throws Exception {
    try {
        threadState.bind();
        return doCall(this.callable);
    } finally {
        threadState.restore();
    }
}

1.绑定方法是必要的,但恢复是为什么?

public void bind() {
    SecurityManager securityManager = this.securityManager;
    if ( securityManager == null ) {
        //try just in case the constructor didn't find one at the time:
        securityManager = ThreadContext.getSecurityManager();
    }
    this.originalResources = ThreadContext.getResources();
    ThreadContext.remove();
    ThreadContext.bind(this.subject);
    if (securityManager != null) {
        ThreadContext.bind(securityManager);
    }
}
public void restore() {
    ThreadContext.remove();
    if (!CollectionUtils.isEmpty(this.originalResources)) {
        ThreadContext.setResources(this.originalResources);
    }
}

2.原始资源是用来做的? 每次进入 AbstractShiroFilter 都会创建一个新的主题并调用它的执行方法,原始资源似乎毫无用处。

常规线程运行状况。 您需要清理资源,以防线程被重用(很常见(。它也将有助于垃圾收集。

你去远足吗?不留痕迹;)

最新更新