This section gives a piece of code,
1 public classTestClass {2 Privatehashmap<string, integer> map =Newhashmap<string, integer>();3 public synchronized voidAdd (String key) {4Integer value =Map.get (key);5 if(value = =NULL){6Map.put (key,1);7}Else{8Map.put (key, value + 1);9 }Ten } one}
As you can see, this is a write synchronization for a container that does not guarantee thread Safety. The context before the code in the book is This:
。。。 however, one thing to mention here is that sometimes by locking the code that uses the thread unsafe container into code that uses a Thread-safe container, you encounter a trap that the author has encountered before, that is, in an example that uses a map to store information after a total count, the value integer in the map uses a Thread-unsafe The HASHMAP code is written like this ...
This shows that the code at the beginning of the paste is correct and recommended to Use. But then the author posted a code here and described
。。。 If we use Concurrenthashmap to replace HashMap, and just remove the synchronized keyword, then there is a problem. The problem is not complicated, you can think about the answer by YOURSELF.
The code is as follows
1 public classTestClass {2 Privateconcurrenthashmap<string, integer> map =Newconcurrenthashmap<string, integer>();3 public voidAdd (String key) {4Integer value =Map.get (key);5 if(value = =NULL){6Map.put (key,1);7}Else{8Map.put (key, value + 1);9 }Ten } one}
The concurrenthashmap here is a Java 1.5 version of the Thread-safe container, with a segmented lock (lock Striking) for fine-grained synchronization, for concurrent access, the operation of the throughput is not greatly compromised.
At first glance there seems to be nothing wrong with choosing a thread-safe container so that the monitor can be removed and there is nothing wrong with it?
It's not going to be a problem, is it? (confirm + 1)
It's not going to be a problem, is it? (confirm Again)
If it is not the author mentioned that this is a trap, I as a small rookie in the future there will be 120% of the potential to fall in this hole ... So let's analyze what's wrong with this code!
As you can see, this Add method is a check then act action, which creates a common race condition. Take a look at a StackOverflow explanation:
A race condition occurs when both or more threads can access shared data and they try-to-change it at the same time. Because the thread scheduling algorithm can swap between threads at any time and you don ' t know the order in which the thread S would attempt to access the shared Data. Therefore, The result of the change in data was dependent on the thread scheduling algorithm, i.e. both threads is "racing "to Access/change the Data.
Problems often occur when one thread does a "check-then-act" (E.G. "check" if the value was X, then "act" to do something t Hat depends on the value being X) and another thread does something to the value in between the "check" and the "act". E.g:
The following is also a concise code to illustrate this phenomenon:
if // the "Check" { // the "Act" // If another thread changed x in between "If (x = = 5)" and "y = x * 2" above, // y'll not is equal to.}
It says that concurrent access or update operations, if not lock control synchronization, The If code is not necessarily the prerequisite of the explicit check, perhaps other threads have already manipulated the Data. It is obvious that a friend with a bit of concurrency awareness will probably understand it. Hee Hee
By this code trap let the author think of some concepts, on the one hand, this article refers to the synchronization of Access operations, on the other hand, is the release of this map, that is often said to expose the reference, to pay attention to even if this method is locked, when other code can also access to update the map, will still lead to the same trap. of course, it is better to directly
Synchronized (map) {... }, Lock the map directly and avoid using the Code's built-in LOCK.
"reading notes", "large Web site system and Java Middleware practice" Chapter 1.2.2.3 Synchronization Traps