Example usage for java.lang String notifyAll

List of usage examples for java.lang String notifyAll

Introduction

In this page you can find the example usage for java.lang String notifyAll.

Prototype

@HotSpotIntrinsicCandidate
public final native void notifyAll();

Source Link

Document

Wakes up all threads that are waiting on this object's monitor.

Usage

From source file:org.alfresco.wcm.client.impl.AbstractCachingSectionFactoryImpl.java

/**
 * Refreshes the section cache if empty or expired.
 * /* w  ww .j a  va 2s .c o m*/
 * @param rootSectionId
 *            the id of the parent web root
 */
private void refreshCacheIfRequired(String rootSectionId) {
    SectionCache cache = rootSectionsByWebsite.get(rootSectionId);
    while (cache == null || cache.isExpired()) {
        String existingSectionToken = sectionsBeingLoaded.putIfAbsent(rootSectionId, rootSectionId);
        if (existingSectionToken == null) {
            try {
                //It looks like we have to load this section tree, 
                //but before we do, let's just check that another thread hasn't got in between us checking the cache
                //and checking whether it's already being loaded...
                cache = rootSectionsByWebsite.get(rootSectionId);
                if (cache != null && !cache.isExpired()) {
                    //This section is now in the cache, so we don't need to do anything 
                    return;
                }
                if (log.isDebugEnabled()) {
                    log.debug(Thread.currentThread().getName() + " started refreshing tree cache for section "
                            + rootSectionId);
                }

                //This section isn't currently being loaded. Load it.
                Map<String, Section> sections = findSectionWithChildren(rootSectionId);
                Section rootSection = sections.get(rootSectionId);
                SectionCache cachedRootSection = new SectionCache(rootSection);
                rootSectionsByWebsite.put(rootSectionId, cachedRootSection);
                sectionsById.putAll(sections);
                if (log.isDebugEnabled()) {
                    log.debug(Thread.currentThread().getName() + " finished refreshing tree cache for section "
                            + rootSectionId);
                }
                return;
            } finally {
                //There may be other threads waiting for us to finish loading this section tree.
                //Let them know that we've finished
                synchronized (rootSectionId) {
                    sectionsBeingLoaded.remove(rootSectionId);
                    rootSectionId.notifyAll();
                }
            }
        } else {
            //This section is currently being loaded
            if (cache != null) {
                //We are currently refreshing the cache, but the requested section does already 
                //appear in the cache. Therefore we'll let the caller simply use the currently-cached 
                //copy
                return;
            } else {
                //This section isn't currently cached, but is already being loaded
                //Wait for it to be loaded...
                synchronized (existingSectionToken) {
                    if (sectionsBeingLoaded.containsKey(existingSectionToken)) {
                        try {
                            if (log.isDebugEnabled()) {
                                log.debug(Thread.currentThread().getName()
                                        + " started waiting for section tree to be loaded " + rootSectionId);
                            }
                            existingSectionToken.wait();
                        } catch (InterruptedException e) {
                        }
                        if (log.isDebugEnabled()) {
                            log.debug(Thread.currentThread().getName()
                                    + " finished waiting for section tree to be loaded " + rootSectionId);
                        }
                    }
                }
            }
        }
        cache = rootSectionsByWebsite.get(rootSectionId);
    }
}