Challenge ConcurrentMap
Task:
Realization of multithreaded system of accounting of site visits.
Description:
It is necessary to create a program to keep track of the number of visits to different pages of the site. The program should work correctly in a multithreaded environment, where several threads can simultaneously increase visit counters for different pages.
Requirements:
- Use
ConcurrentHashMapto store data about page visits; - Implement a method that increments the visit count for a given page.
incrementVisit(); - Implement a method that returns the current visit count for a given page.
getVisitCount(); - Create multiple threads that will increment the visit counts simultaneously. (This is implemented in the
Mainclass, you need to figure out what's going on there).
Hints:
- Use
ConcurrentHashMapto store data where the key is the URL of the page and the value is the visit count; - Use
compute()ormerge()methods to atomically update counters inConcurrentHashMap.
You need to complete the methods in the PageVisitCounterImpl class, and then run the tests in the PageVisitCounterTest class.
After you have successfully completed the task, all tests should pass.
Note
After you have done everything correctly, you can try to edit the line where
ConcurrentMapis declared in thePageVisitCounterImplclass and see what happens.
That is, you need to replace this line here with the ConcurrentMap implementation.
Main.java
1private final ConcurrentMap<String, Integer> visitCounts = new ConcurrentHashMap<>();
On this one, with the realization of the usual Map
Main.java
1private final Map<String, Integer> visitCounts = new HashMap<>();
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 3.33
Challenge ConcurrentMap
Stryg for at vise menuen
Task:
Realization of multithreaded system of accounting of site visits.
Description:
It is necessary to create a program to keep track of the number of visits to different pages of the site. The program should work correctly in a multithreaded environment, where several threads can simultaneously increase visit counters for different pages.
Requirements:
- Use
ConcurrentHashMapto store data about page visits; - Implement a method that increments the visit count for a given page.
incrementVisit(); - Implement a method that returns the current visit count for a given page.
getVisitCount(); - Create multiple threads that will increment the visit counts simultaneously. (This is implemented in the
Mainclass, you need to figure out what's going on there).
Hints:
- Use
ConcurrentHashMapto store data where the key is the URL of the page and the value is the visit count; - Use
compute()ormerge()methods to atomically update counters inConcurrentHashMap.
You need to complete the methods in the PageVisitCounterImpl class, and then run the tests in the PageVisitCounterTest class.
After you have successfully completed the task, all tests should pass.
Note
After you have done everything correctly, you can try to edit the line where
ConcurrentMapis declared in thePageVisitCounterImplclass and see what happens.
That is, you need to replace this line here with the ConcurrentMap implementation.
Main.java
1private final ConcurrentMap<String, Integer> visitCounts = new ConcurrentHashMap<>();
On this one, with the realization of the usual Map
Main.java
1private final Map<String, Integer> visitCounts = new HashMap<>();
Tak for dine kommentarer!