• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

synchronize in Collections not in Threads in Core Java

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I searched in most of the forums, for Synchronize concept in Core Java. 99% of replies are for "Synchronization" in "Threads" Topic, but I didnt got answer for my question anywhere...

In Collections, when we are discussing about differences of Hashtable and Hashmap:
one of the difference is Hashtable is Synchornized, but Hash map is not and due to this Hashtable is not good in performance, when compared with Hashmap.

Do any one explains the concept of Synchronize in Collection point of view?

Thanks,
Mallikkannan D

Have a Great day ahead...
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The synchronization being discussed there is the same as synchronization in threads. Some collections have synchronized methods to make the more thread safe. Others do not.. There is some overhead associated with synchronization and so in a single threaded environment those collections are needlessly synchronized and less efficient than their newer counterparts.

And in a multi threaded environment they don't do a complete job of synchronization so you have to do your own additional synchronization. There are better thread-safe options for collections than Hashtable and Vector, so they don't make sense in a multi threaded environment either.

Finally, Hashtable and Vector were around before the whole collections framework was developed, and were sort of retrofitted into the framework. they never really fit properly into the framework itself ( having enumerators for example ). I think of this point as being the best reason for not using Hashtable and Vector. The collections framework is really flexible, but you lose some of that when you are tied to the old classes.

The point about synchronization and it's efficiency losses is minor, it isn't that expensive to tack synchronization on in a single threaded environment, because the cases where it costs the most is in multi threaded environments. And in those environments it isn't a question of to synchronize or not, but rather the method and manner you use for synchronization. And there are frankly much better options available.

 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mallik kannan,

Welcome to CodeRanch!

Yes, I agree with Steve Luke, that Hashtable and Vector are now depricated. Instead, proper way to use them as thread-safe would be ConcurrentHashMap and Collections.synchronizedList respectively.

However, technically, when we say that some collection or a class is synchronized, all that means is - their data manipulator methods are synchronized. Which means, if more threads start manipulating same object, the operation would be thread-safe.

So, bottom line is, synchronization is related to thread itself.

I hope this helps.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
synchronization is always used in the context of threads (multithreading) where multiple threads are working on the same shared data and it is used to make sure that if one thread is working on shared data, no other thread can access that data.

As per your question Hashtable (though becoming obsolete now) is synchronized while HashMap is not. What it means that multiple threads can work on a HashTable object safely while this is not the case with HashMap object.

You can see from the code of HashTable (http://www.docjar.com/html/api/java/util/Hashtable.java.html) that various methods(put,get,size() etc) are synchronized :

429 public synchronized V put(K key, V value) {
...

461 }


355 public synchronized V get(Object key) {
...
365 }


while this is not in HashMap.
 
Marshal
Posts: 79969
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are exaggerating to say that Hashtable (and Vector) are obsolete, and they are not deprecated.
It might be better to regard them as legacy code, best replaces in new code by HashMap and ArrayList. Similary StringBuffer and StringBuilder.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Collection Point of view Synchronized means only one one thread can modify a hash table at one point of time. Any thread before performing an update on a hashtable will have to acquire a lock on the object
 
I've read about this kind of thing at the checkout counter. That's where I met this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic