Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Which One is optimal to choose ?

Syncronized hash map or Hash table with single thread model?

How can a hash map syncronized with out using syncrozed
blocks in programm?

Answer Posted / ranganathkini

The java.util.Hashtable provides a synchronized collection
implementation to store key-value pairs. Hence
synchronization of a Hashtable is not necessary.

Synchronization has a performance overhead and therfore must
be used with caution in only those situations where a
resource(s) is shared by multiple threads. Hence a
java.util.HashMap implementation can be used for single
threaded model and it can be externally synchronized if it
is to be used in a mult-threaded model.

To get a synchronized version of a java.util.HashMap without
using synchronized blocks, do this:

// make sure this is in imports
import java.util.*;

// our unsynchronized map object
Map myMap = new HashMap();

// get a synchronized map from our unsynchronized map
Map mySyncMap = Collections.synchronizedMap( myMap );

After this, all access to the map must be via mySyncMap
reference and NOT myMap.

Another way to acquire a synchronized map is:

Map mySyncMap = Collections.synchronizedMap( new HashMap() );

Is This Answer Correct ?    5 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why for each loop is used?

960


Define how does a try statement determine which catch clause should be used to handle an exception?

1066


What is the purpose of the enableevents() method in java programming?

1081


Can you instantiate the math class?

991


How do you find the independent variable?

982


What are the basic concepts of OOPS in java?

971


How many arguments can a method have java?

976


How does predicate work in java?

937


How can constructor chaining be done using this keyword?

1210


How do you empty a list in java?

924


What are user defined exceptions?

963


Define how can we find the actual size of an object on the heap?

1113


What is thread safe java?

868


How do you know if a value is nan?

1008


How will you reverse a singly-link list?

938