private final ThreadLocal<Set<EncodedResource>> resourcesCurrentlyBeingLoaded =
??? ??? ??? new NamedThreadLocal<Set<EncodedResource>>("XML bean definition resources currently being loaded");
?
概括起来说,对于多线程资源共享的问题,同步机制采用了“以时间换空间”的方式,而ThreadLocal采用了“以空间换时间”的方式。前者仅提供一份变量,让不同的线程排队访问,而后者为每一个线程都提供了一份变量,因此可以同时访问而互不影响。
?
Assert.notNull(encodedResource, "EncodedResource must not be null");
currentResources = new HashSet<EncodedResource>(4);
if (!currentResources.add(encodedResource)) {
??? ??? ??? throw new BeanDefinitionStoreException(
??? ??? ??? ??? ??? "Detected cyclic loading of " + encodedResource + " - check your import definitions!");
??? ??? }
?
?finally {
?? ??? ??? ?currentResources.remove(encodedResource);
?? ??? ??? ?if (currentResources.isEmpty()) {
?? ??? ??? ??? ?this.resourcesCurrentlyBeingLoaded.remove();
?? ??? ??? ?}
?? ??? ?}