Mercurial > dive4elements > gnv-client
annotate gnv-artifacts/src/main/java/de/intevation/gnv/utils/ExclusiveExec.java @ 800:db5b04ecb426
ISSUE215: Improved ODV-Export. now all Columns which have identical values but different Parameters will be merged to one row.
gnv-artifacts/trunk@882 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Tue, 06 Apr 2010 13:07:11 +0000 |
parents | c4156275c1e1 |
children | 2cea76f1112e |
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 |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
7 * synchronization, you first need to do call {@link #acquire(String)} to |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
8 * 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 |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
11 * #acquire(String)}. A thread needs to wait for another thread if their keys |
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 { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
19 public static final ExclusiveExec INSTANCE = new ExclusiveExec(); |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
20 |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
21 private HashMap tokens; |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
22 |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
23 public static class UniqueKey { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
24 Object key; |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
25 int [] refs; |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
26 public UniqueKey(Object key) { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
27 this.key = key; |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
28 refs = new int[1]; |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
29 } |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
30 } |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
31 |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
32 /** |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
33 * Private constructor. Use {@link #INSTANCE} instead. |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
34 */ |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
35 private ExclusiveExec() { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
36 tokens = new HashMap(); |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
37 } |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
38 |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
39 /** |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
40 * 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
|
41 * block. |
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 * @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
|
44 * |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
45 * @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
|
46 * 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
|
47 */ |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
48 public UniqueKey acquire(Object key) { |
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 try { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
51 UniqueKey internalKey = null; |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
52 synchronized (tokens) { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
53 internalKey = (UniqueKey)tokens.get(key); |
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 if (internalKey == null) { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
56 tokens.put(key, internalKey = new UniqueKey(key)); |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
57 } |
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 |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
60 synchronized (internalKey) { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
61 ++internalKey.refs[0]; |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
62 while (internalKey.refs[0] > 1) { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
63 internalKey.wait(10000L); |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
64 } |
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 |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
67 return internalKey; |
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 catch (InterruptedException ie) { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
70 return null; |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
71 } |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
72 } |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
73 |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
74 /** |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
75 * 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
|
76 * being synchronized. |
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 * @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
|
79 */ |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
80 public void release(UniqueKey internalKey) { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
81 if (internalKey != null) { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
82 synchronized (internalKey) { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
83 if (--internalKey.refs[0] < 1) { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
84 synchronized (tokens) { |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
85 tokens.remove(internalKey.key); |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
86 } |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
87 } |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
88 internalKey.notifyAll(); |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
89 } |
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 } |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
92 } |
eedad2ddad14
Removed race-condition while shapefile creation (issue164).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
93 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |