0

SubjectCallable's call method:

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

1.bind method is necsssary, but restore is why?

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.originalResources is use to do ? each time enter the AbstractShiroFilter will create a new subject and invoke it's execute method, the originalResources seems useless.

lucare
  • 35
  • 6

1 Answers1

0

General thread health. You need to clean up resource in case the thread is re-used (very common). And it would help with garbage collection too.

Do you ever go hiking? Leave no trace ;)

Brian Demers
  • 2,051
  • 1
  • 9
  • 12
  • everything is new when a request come in, the Subject, SubjectCallable and SubjectThreadState, so the originalResources is seems useless, it is belong to current thread, but the current thread is just come in ,no resource put in ThreadContext. – lucare Feb 03 '18 at 03:56
  • https://stackoverflow.com/questions/3869026/how-to-clean-up-threadlocals – Brian Demers Feb 05 '18 at 15:07