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
What is contractor means and methods?
What is the synonym of framework?
What are basic data types?
When will you define a method as static?
Which methods are used during serialization and deserialization process?
What kind of variables a class can consist of?
What is re-factoring in software?
What is autoboxing in java?
What are the 4 types of research methods?
Is java pass by value or pass by reference?
What are the three parts of a lambda expression? What is the type of lambda expression?
What is the final variable?
Is a class an object?
Why there are some null interface in java? What does it mean?
Is java same as core java?