Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

thread safe map for java

Tags:

java

i need a thread safe map, i have something like this: (i'm very new to java)

 public static class Manager         {         static          { //something wrong here, doesn't compile            list = new java.util.Collections           .synchronizedMap(new Map<String, Client>());         }         static Map<String,Client> list;          public static void AddClient(Client client)         {         // thread safe add client to the list         }          public static void RemoveClient(Client client)         {         // thread safe remove client to the list         }          } 
like image 609
Omu Avatar asked Nov 24 '09 18:11

Omu


People also ask

Is Java map put thread-safe?

Java HashMap is not thread-safe and hence it should not be used in multithreaded applications.

Is TreeMap thread-safe?

TreeMap and TreeSet are not thread-safe collections, so care must be taken to ensure when used in multi-threaded programs. Both TreeMap and TreeSet are safe when read, even concurrently, by multiple threads.

How do I make a HashMap thread-safe?

You can make HashMap thread safe by wrapping it with Collections. synchronizedMap() . What's the difference? @naXa ConcurrentHashMap allows concurrent access and a synchronized one doesn't.

Which is faster HashMap or ConcurrentHashMap?

HashMap performance is relatively high because it is non-synchronized in nature and any number of threads can perform simultaneously. But ConcurrentHashMap performance is low sometimes because sometimes Threads are required to wait on ConcurrentHashMap.


1 Answers

java.util.concurrent.ConcurrentHashMap

like image 62
Bozho Avatar answered Sep 25 '22 07:09

Bozho