Home arrow static arrow Java Programming [Archive] - threadlocal problem for newbie
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - threadlocal problem for newbie
This topic has 3 replies on 1 page.

Posts:3
Registered: 8/6/04
threadlocal problem for newbie  
Aug 6, 2004 11:00 PM



 
I'm trying to add a wart onto a very large project with as little mucking with existing code as possible - which is why I'm trying to use threadlocal even though I know it's not the best way to do things...with that said...

I'm trying to implement a per-thread linked list where each thread has a copy of it's "head". I'm trying to use ThreadLocal to do this, but it's not working for me - it works exactly as if I hadn't added the ThreadLocal() stuff at all (all threads are using the same "head".

Can someone point out the problems that are obvious to all but me?

The code is trimmed for simplicity:

public class Cache { 	private Cache next;	private String url;	private String date; 	private static ThreadLocal head = new ThreadLocal() {		private Cache head;		protected Object initialValue() {			return new Cache();		}		public Object get() {			return (Object) head;		}		public void set(Object newvalue) {			head = (Cache) newvalue;		}	}public String findinlist(String url) {	Cache pcache = (Cache) head.get();		while(pcache != null) {			if() //url matches url in pcache)				break;			pcache=pcache.next;		}		if(pcache != null) {			return(pcache.date);		}		else return(null);	} 	public void addtolist(String url, String date) {	String date = findinlist(url);		if(date != null) return;		Cache pcache = new Cache();		pcache.url = url;		pcache.date = date;		pcache.next = (Cache) head.get();		head.set(pcache);		return;	}}
 

Posts:37,103
Registered: 3/30/99
Re: threadlocal problem for newbie  
Aug 7, 2004 12:04 AM (reply 1 of 3)



 
Not sure, but I think if you get rid of the Cache member variable of the ThreadLocal, and only override initialValue--not get or set--then you'll be in better shape.
 

Posts:37,103
Registered: 3/30/99
Re: threadlocal problem for newbie  
Aug 7, 2004 12:07 AM (reply 2 of 3)



 
...the reason being that the default implementations of set and get have knowledge of the Thread, but your overridden versions don't.

There's nothing magical about the ThreadLocal class. It's the implementation of get and set that gives the thread-specific behavior. Here's ThreadLocal's implementation of get:
public Object get() {    Thread t = Thread.currentThread();    ThreadLocalMap map = getMap(t);    if (map != null)         return map.get(this);     // Maps are constructed lazily.  if the map for this thread    // doesn't exist, create it, with this ThreadLocal and its    // initial value as its only entry.    Object value = initialValue();    createMap(t, value);    return value;} 
 

Posts:3
Registered: 8/6/04
Re: threadlocal problem for newbie  
Aug 7, 2004 7:08 AM (reply 3 of 3)



 
Thanks - I was making it harder than it really was.
 
This topic has 3 replies on 1 page.