KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > SyncSection


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.masterfs;
21
22 import java.util.Collections JavaDoc;
23 import java.util.WeakHashMap JavaDoc;
24
25 /**
26  *
27  * @author rm111737
28  */

29 final class SyncSection {
30     /** sync. purpose*/
31     private int exclusiveCounter;
32     private int normalCounter;
33     final private ThreadLocal JavaDoc normalCounterPerThread = new ThreadLocal JavaDoc();
34     final private ThreadLocal JavaDoc exclusiveCounterPerThread = new ThreadLocal JavaDoc();
35     final private Object JavaDoc sync = new Object JavaDoc();
36     private static SyncSection instance;
37
38
39     static SyncSection getDefault() {
40         synchronized (Cache.class) {
41             if (instance == null) {
42                 instance = new SyncSection();
43             }
44         }
45         return instance;
46     }
47
48     /** Creates a new instance of SyncSection */
49     private SyncSection() {
50     }
51
52     void enterSection() {
53         synchronized (sync) {
54             try {
55                 while ((exclusiveCounter - getValue(exclusiveCounterPerThread)) > 0) {
56                     sync.wait();
57                 }
58
59                 normalCounter++;
60                 setValue(normalCounterPerThread, getValue(normalCounterPerThread) + 1);
61             } catch (InterruptedException JavaDoc iex) {
62                 iex.printStackTrace();
63             }
64         }
65     }
66
67     void finishSection() {
68         synchronized (sync) {
69             normalCounter--;
70             setValue(normalCounterPerThread, getValue(normalCounterPerThread) - 1);
71             sync.notifyAll();
72         }
73
74     }
75
76     /** there should be prevented to call exclusive section from norma sectin, because of
77      * danger of deadlock. Thread, that enters exclusive section should avoid to keep
78      * locks, that could prevent to finish normal section.
79      */

80     void enterExclusiveSection() {
81         synchronized (sync) {
82             try {
83                 while ((normalCounter - getValue(normalCounterPerThread)) > 0 ||
84                         (exclusiveCounter - getValue(exclusiveCounterPerThread)) > 0) {
85                     sync.wait();
86                 }
87             } catch (InterruptedException JavaDoc iex) {
88                 iex.printStackTrace();
89             }
90             exclusiveCounter++;
91             setValue(exclusiveCounterPerThread, getValue(exclusiveCounterPerThread) + 1);
92
93         }
94     }
95
96     void finishExclusiveSection() {
97         synchronized (sync) {
98             exclusiveCounter--;
99             setValue(exclusiveCounterPerThread, getValue(exclusiveCounterPerThread) - 1);
100             sync.notifyAll();
101         }
102     }
103
104     private int getValue(ThreadLocal JavaDoc thrVal) {
105         Integer JavaDoc val = (Integer JavaDoc) thrVal.get();
106         return (val != null) ? val.intValue() : 0;
107     }
108
109     private void setValue(ThreadLocal JavaDoc thrVal, int value) {
110         thrVal.set(new Integer JavaDoc(value));
111     }
112
113 }
114
Popular Tags