KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > util > HashThreadLocal


1 package org.openejb.util;
2
3 import java.util.HashMap JavaDoc;
4
5 /*
6 * This variation of ThreadLocal accomplishes thread-specific storage by thread as well
7 * as by object. Values are associated with both an key and a thread, which allows
8 * each value to stored specific to an object and thread.
9 *
10 * @see org.openejb.resource.SharedLocalConnectionManager
11 * @author <a HREF="richard@monson-haefel.com">Richard Monson-Haefel</a>
12 * @version $Rev: 1921 $ $Id: HashThreadLocal.java 1921 2005-06-19 22:40:34Z jlaskowski $
13 */

14 public class HashThreadLocal {
15     HashMap JavaDoc keyMap = new HashMap JavaDoc();
16     public synchronized void put(Object JavaDoc key, Object JavaDoc value){
17         FastThreadLocal threadLocal = (FastThreadLocal)keyMap.get(key);
18         if(threadLocal==null){
19             threadLocal = new FastThreadLocal();
20             keyMap.put(key, threadLocal);
21         }
22         threadLocal.set(value);
23     }
24     public synchronized Object JavaDoc get(Object JavaDoc key){
25         FastThreadLocal threadLocal = (FastThreadLocal)keyMap.get(key);
26         if(threadLocal==null)return null;
27         return threadLocal.get();
28     }
29 }
Popular Tags