annotate gnv-artifacts/src/main/java/de/intevation/gnv/utils/ExclusiveExec.java @ 1062:58b4a07db856

Cach improvement: remove the cached elements of each visited state that is visited while stepping back to a previous state. gnv-artifacts/trunk@1147 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 02 Jun 2010 09:52:39 +0000
parents 22c18083225e
children f953c9a559d8
rev   line source
775
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
1 package de.intevation.gnv.utils;
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
2
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
3 import java.util.HashMap;
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
4
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
5 /**
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
6 * This class can be used to synchronize threads with a given key. To use this
815
22c18083225e Removed compiler warnings while JavaDoc generation.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 806
diff changeset
7 * synchronization, you first need to do call {@link #acquire(java.lang.Object)}
22c18083225e Removed compiler warnings while JavaDoc generation.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 806
diff changeset
8 * to retrieve a {@link UniqueKey}. After this, you can call the code being
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 775
diff changeset
9 * synchronized. After this execution, you need to call
775
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
10 * {@link #release(UniqueKey)} with your token you retrieved from {@link
815
22c18083225e Removed compiler warnings while JavaDoc generation.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 806
diff changeset
11 * #acquire(java.lang.Object)}. A thread needs to wait for another thread if their keys
775
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
12 * are equal. Threads with different keys don't need to wait for each other.
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
13 *
780
c4156275c1e1 Bring @author javadoc tags in form '@author <a href="john.doe@example.com">John Doe</a>'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
14 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
c4156275c1e1 Bring @author javadoc tags in form '@author <a href="john.doe@example.com">John Doe</a>'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
15 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
775
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
16 */
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
17 public final class ExclusiveExec
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
18 {
806
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
19 /**
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
20 * The only instance of this singleton.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
21 */
775
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
22 public static final ExclusiveExec INSTANCE = new ExclusiveExec();
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
23
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
24 private HashMap tokens;
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
25
806
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
26 /**
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
27 * This class represents a unique key with a reference counter.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
28 */
775
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
29 public static class UniqueKey {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
30 Object key;
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
31 int [] refs;
806
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
32
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
33 /**
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
34 * Constructs a new UniqueKey.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
35 *
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
36 * @param key The key of this unique key.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
37 */
775
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
38 public UniqueKey(Object key) {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
39 this.key = key;
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
40 refs = new int[1];
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
41 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
42 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
43
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
44 /**
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
45 * Private constructor. Use {@link #INSTANCE} instead.
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
46 */
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
47 private ExclusiveExec() {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
48 tokens = new HashMap();
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
49 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
50
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
51 /**
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
52 * This method serves a {@link UniqueKey} and starts a synchronized code
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
53 * block.
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
54 *
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
55 * @param key The key used to identify same threads.
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
56 * @return UniqueKey. Use this object to call {@link #release(UniqueKey)}
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
57 * at the end of your code being synchronized.
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
58 */
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
59 public UniqueKey acquire(Object key) {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
60
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
61 try {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
62 UniqueKey internalKey = null;
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
63 synchronized (tokens) {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
64 internalKey = (UniqueKey)tokens.get(key);
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
65
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
66 if (internalKey == null) {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
67 tokens.put(key, internalKey = new UniqueKey(key));
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
68 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
69 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
70
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
71 synchronized (internalKey) {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
72 ++internalKey.refs[0];
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
73 while (internalKey.refs[0] > 1) {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
74 internalKey.wait(10000L);
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
75 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
76 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
77
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
78 return internalKey;
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
79 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
80 catch (InterruptedException ie) {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
81 return null;
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
82 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
83 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
84
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
85 /**
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
86 * This method releases a lock. Call this method at the end of your code
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
87 * being synchronized.
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
88 *
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
89 * @param internalKey Token retrieved by {@link #acquire(Object)}.
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
90 */
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
91 public void release(UniqueKey internalKey) {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
92 if (internalKey != null) {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
93 synchronized (internalKey) {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
94 if (--internalKey.refs[0] < 1) {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
95 synchronized (tokens) {
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
96 tokens.remove(internalKey.key);
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
97 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
98 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
99 internalKey.notifyAll();
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
100 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
101 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
102 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
103 }
eedad2ddad14 Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
104 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org