dcsimg
How to use synchronizedMap for HashMap
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   kraja_kraja
Posted On:   Sunday, July 14, 2002 08:03 AM

How to use synchronizedMap for HashMap I wrote a class as follows public class MyMap extends HashMap { ...... } Now i realize the HashMap is not synchronized. I will be instanting MyMap class and i will be inserting data and this MyMap will be accessed by Multiple threads. Hence that warrants me to use synchronization. Now will the following thing work? public class MyMap extends HashMap ( public MyMap() { this = Collections.synchronizedMap(this); } } Is the above code correct ? Now if HashMap is a data member of MyMap call , then i think i might now how to use syn   More>>

How to use synchronizedMap for HashMap

I wrote a class as follows

public class MyMap extends HashMap
{
......
}

Now i realize the HashMap is not synchronized. I will be instanting MyMap class and i will be inserting data and this MyMap will be accessed by Multiple threads. Hence that warrants me to use synchronization.
Now will the following thing work?

public class MyMap extends HashMap
(
public MyMap()
{
this = Collections.synchronizedMap(this);
}
}

Is the above code correct ?










Now if HashMap is a data member of MyMap call , then i think i might now how to use synchronizedMap

public class MyMap
{
public HashMap tempMap;
public MyMap() {
tempMap = Collections.synchronizedMap(new HashMap());
}
}


Can someone help me on this. I would really appreciate all the help




Please use html tags to format code blocks.

   <<Less

Re: How to use synchronizedMap for HashMap

Posted By:   Shawn_Alexander  
Posted On:   Monday, July 15, 2002 07:36 AM

Kraja,



Why not use a Hashtable? The synchronization is already provided for you and as of the JDK 1.2, Hashtables implement the Map interface so you can return it as a Map. So, if you want to synchronize all access to the Map, use Hashtable.



Shawn
About | Sitemap | Contact