48
loading...
This website collects cookies to deliver better user experience
-src
-main/java/com/example/concurrencysample
-BasicCHeckMap.java
-BasicSynchronization.java
-Concurrency.java
-ConcurrencyCheckout.java
-SingletonRepo.java
-Util.java
-test/java/com/example/concurrencysample
-SingletonRepoTests.java
public Concurrency putKey(int product_code ) {
Date date = Util.getRandomDate();
if (map.containsKey(product_code)) {
return map.get(product_code);
}else{
return map.put(product_code, new Concurrency(product_code,"test" , date));
}
}
If the HashMap already has the same value, don’t put it.
BasicSynchronization calls pukey() of SingletonRepo and inserts a randomly generated number between 0 ~ 2000.
ConcurrencyCheckout is Main class. This class creates 5 BasicSynchronizations and starts 5 threads.
This code has a concurrency issue. if it call two PutKey() from two threads at the same time. What will happen?
The number is entered only once, but the call count is increased twice.
HashMap is not thread-safe. The code above is dangerous because get and put don’t guarantee atomicity.
If the number of threads is increased and the number of put calls executed simultaneously increases, the result cannot be guaranteed.
An error occurs because the same problem appears in the UnitTest being executed. SingletonRepotests, a unit test that is performed, creates 8 threads and raises the count one by one when an input occurs in the HashMap. And after all the work is done, the number of values entered in the HashMap is compared with the number of occurrences. If the values are the same, the number of inputs and the number of HashMaps are the same, so the function worked correctly.
However, UnitTest will continue to fail because the current code was programmed without considering concurrency. Of course, if you are very lucky, the test may succeed. However, to prevent this, SingletonRepotests performs the same test 100 times.
You can change HashMap to ConcurrentHashMap. ConcurrentHashMap is designed to be thread-safe, so it operates stably in a multi-threaded environment and has slightly better performance than Hashtable and synchronized map. ConcurrentHashMap locks the map when you put it, so you can prevent the same key put at the same time.
Open the SingletonRepo.java file in Cloud9, then change the HashMap below to ConcurrentHashMap.
+ import java.util.concurrent.*;
...
- private HashMap<Integer, Concurrency> map = new HashMap<>();
+ private ConcurrentHashMap<Integer, Concurrency> map = new ConcurrentHashMap<>();
git add .
git commit -m "fix singletonrepo.java"
git push