alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.masterfs;

import java.util.Collections;
import java.util.WeakHashMap;

/**
 *
 * @author  rm111737
 */
final class SyncSection {
    /** sync. purpose*/
    private int exclusiveCounter;
    private int normalCounter;
    final private ThreadLocal normalCounterPerThread = new ThreadLocal();
    final private ThreadLocal exclusiveCounterPerThread = new ThreadLocal();
    final private Object sync = new Object();
    private static SyncSection instance;


    static SyncSection getDefault() {
        synchronized (Cache.class) {
            if (instance == null) {
                instance = new SyncSection();
            }
        }
        return instance;
    }

    /** Creates a new instance of SyncSection */
    private SyncSection() {
    }

    void enterSection() {
        synchronized (sync) {
            try {
                while ((exclusiveCounter - getValue(exclusiveCounterPerThread)) > 0) {
                    sync.wait();
                }

                normalCounter++;
                setValue(normalCounterPerThread, getValue(normalCounterPerThread) + 1);
            } catch (InterruptedException iex) {
                iex.printStackTrace();
            }
        }
    }

    void finishSection() {
        synchronized (sync) {
            normalCounter--;
            setValue(normalCounterPerThread, getValue(normalCounterPerThread) - 1);
            sync.notifyAll();
        }

    }

    /** there should be prevented to call exclusive section from norma sectin, because of
     * danger of deadlock. Thread, that enters exclusive section should avoid to keep
     * locks, that could prevent to finish normal section.
     */
    void enterExclusiveSection() {
        synchronized (sync) {
            try {
                while ((normalCounter - getValue(normalCounterPerThread)) > 0 ||
                        (exclusiveCounter - getValue(exclusiveCounterPerThread)) > 0) {
                    sync.wait();
                }
            } catch (InterruptedException iex) {
                iex.printStackTrace();
            }
            exclusiveCounter++;
            setValue(exclusiveCounterPerThread, getValue(exclusiveCounterPerThread) + 1);

        }
    }

    void finishExclusiveSection() {
        synchronized (sync) {
            exclusiveCounter--;
            setValue(exclusiveCounterPerThread, getValue(exclusiveCounterPerThread) - 1);
            sync.notifyAll();
        }
    }

    private int getValue(ThreadLocal thrVal) {
        Integer val = (Integer) thrVal.get();
        return (val != null) ? val.intValue() : 0;
    }

    private void setValue(ThreadLocal thrVal, int value) {
        thrVal.set(new Integer(value));
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.