0

Hi I am new for working on Java multi thread environment. I have to add,update and remove the set of objects in collection at one thread. And at this same time I going to check and iterate the object at another thread.

List and set are not thread safe. Can any one please share me that which one of the collection/Map classes are better to use on multi thread environment ?

Murali
  • 341
  • 2
  • 7
  • 24
  • 1
    provide more details about the type of collection. – UmNyobe Dec 27 '13 at 09:46
  • 4
    http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/package-summary.html ... I mean, you even *tagged* this question with the answer (Who upvotes this?). – Brian Roach Dec 27 '13 at 09:47
  • 1
    The single threaded collections are still better to use if you can ensure that only one threaded will every access that collection. The answer is; it depends. ConcurrentHashMap is great but is much larger and a bit slower than HashMap, only use it if you need to. – Peter Lawrey Dec 27 '13 at 09:56

6 Answers6

6

Yes,

There s ConcurrentHashMap<K,V> in java, you can use it.

A hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates.

More over Collections utility class have methods to synchronize.

Collections.synchronizedCollection(YourCollection);

And other methods like

synchronizedList(List<T> list)

synchronizedSet(Set<T> s)

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Hi suresh, In my case i don't need to use key and pair values. – Murali Dec 27 '13 at 09:46
  • @Murali Yes Murali. Edited my post. – Suresh Atta Dec 27 '13 at 09:51
  • Even if you use synchronizedSet(Set s) or synchronizedList(List list) , please make sure you synchronized the return set.Refer https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedSet(java.util.Set) – user2452561 Jan 04 '19 at 00:43
1

May be vector in case you dont want to save key-value or "Collections.synchronizedList().

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

You could use a concurrent hash set created from the ConcurrentHashMap:

 Set<K> set = Collections.newSetFromMap(new ConcurrentHashMap<K, Object>());

This collection is thread-safe and has the features of the ConcurrentHashMap - it's faster than Collections.synchronized* wrappers.

Be aware though that thread safety for a collection doesn't guarantee thread-safety for it's items. In order to achieve the least a common possible approach is to make the items read-only: i.e. immutable or returning copies on access.

Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
1

Use Collections API

public static <T> Set<T> synchronizedSet(Set<T> s)

Returns a synchronized (thread-safe) set backed by the specified set

public static <T> List<T> synchronizedList(List<T> list)

Returns a synchronized (thread-safe) list backed by the specified list

You can pass your List or Set implementation to the above methods and get a thread-safe one

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
0

Look into the java.util.concurrent package, it has several thread safe collections

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
0

You can use CopyOnWriteArrayList and CopyOnWriteArraySet.
Read about it here as well.

Alex
  • 11,451
  • 6
  • 37
  • 52