KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > lock > ThreadLocalMap


1 package org.jboss.cache.lock;
2
3 import java.util.Collection JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Set JavaDoc;
7
8 /**
9  * Map which reduces concurrency and potential memory leaks for non-static ThreadLocals.
10  * http://www.me.umn.edu/~shivane/blogs/cafefeed/2004/06/of-non-static-threadlocals-and-memory.html
11  * @author Brian Dueck
12  * @version $Id: ThreadLocalMap.java,v 1.1 2005/06/17 05:49:03 bela Exp $
13  */

14 public class ThreadLocalMap implements Map JavaDoc {
15
16     private ThreadLocal JavaDoc threadLocal = new ThreadLocal JavaDoc();
17
18     private Map JavaDoc getThreadLocalMap() {
19         Map JavaDoc map = (Map JavaDoc) threadLocal.get();
20         if (map == null) {
21             map = new HashMap JavaDoc();
22             threadLocal.set(map);
23         }
24         return map;
25     }
26
27     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
28         return getThreadLocalMap().put(key, value);
29     }
30
31     public Object JavaDoc get(Object JavaDoc key) {
32         return getThreadLocalMap().get(key);
33     }
34
35     public Object JavaDoc remove(Object JavaDoc key) {
36         return getThreadLocalMap().remove(key);
37     }
38
39     public int size() {
40         return getThreadLocalMap().size();
41     }
42
43     public void clear() {
44         getThreadLocalMap().clear();
45     }
46
47     public boolean isEmpty() {
48         return getThreadLocalMap().isEmpty();
49     }
50
51     public boolean containsKey(Object JavaDoc arg0) {
52         return getThreadLocalMap().containsKey(arg0);
53     }
54
55     public boolean containsValue(Object JavaDoc arg0) {
56         return getThreadLocalMap().containsValue(arg0);
57     }
58
59     public Collection JavaDoc values() {
60         return getThreadLocalMap().values();
61     }
62
63     public void putAll(Map JavaDoc arg0) {
64         getThreadLocalMap().putAll(arg0);
65     }
66
67     public Set JavaDoc entrySet() {
68         return getThreadLocalMap().entrySet();
69     }
70
71     public Set JavaDoc keySet() {
72         return getThreadLocalMap().keySet();
73     }
74
75 }
Popular Tags