1. Use the synchronized keyword.
Synchronized(Anobject ){
Value = map. Get (key );
}
Ii. Use the locks provided by JDK (Java. util. Concurrent. locks. Lock ).
Lock. Lock ();
Value = map. Get (key );
Lock. Unlock ();
3. Use the read/write lock (Java. util. Concurrent. locks. readwritelock) provided by jdk1.5 ).
Rwlock. readlock (). Lock ();
Value = map. Get (key );
Rwlock. readlock (). Unlock ();
In this way, the two read operations can be performed simultaneously, and the theoretical efficiency will be higher than that of method 2.
4. Use the java. util. Concurrent. concurrenthashmap class provided by jdk1.5.
This class divides the storage space of map into several blocks, each of which has its own lock, greatly reducing the competition for the same lock by multiple threads.
Value = map. Get (key );//The synchronization mechanism is embedded in the get method.