• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronized and unsynchronized

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hashtable is synchronized while Hashmap is not. That neans Hashtable is thread safe while Hashmap is not.

Why would someonne use unsynchronized method or class iun place of synchronized ?
WHats the scenario in which unsynchronized ios preferred over synchronized ? Whats the trade off when we are using synchronized in place of unsynchronized ?

Thanks
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nirjari patel wrote:
Why would someonne use unsynchronized method or class iun place of synchronized ?
WHats the scenario in which unsynchronized ios preferred over synchronized ? Whats the trade off when we are using synchronized in place of unsynchronized ?


local variable. confused? somebody handover a method to you .
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Synchronization has a small performance overhead, so if it is not necessary, you'd rather not want to do it.

Most of the time you don't need synchronization - you need it only if your program has multiple threads that might be using the HashMap at the same time. So most of the time, the synchronization is done for nothing and it costs a little bit of performance.

If you do need a synchronized map, you can use the static method synchronizedMap(...) in class Collections to add synchronization to a regular map. Don't use the legacy collection classes, such as Hashtable. Example:

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or use a class from the java.util.concurrent package such as ConcurrentHashMap
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic