Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge ConcurrentMap | Synchronized Collections
Multithreading in Java
course content

Course Content

Multithreading in Java

Multithreading in Java

1. Multithreading Basics
2. Synchronized Collections
3. High-level Synchronization Mechanisms
4. Multithreading Best Practices

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 ConcurrentHashMap to 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 Main class, you need to figure out what's going on there).

Hints:

  • Use ConcurrentHashMap to store data where the key is the URL of the page and the value is the visit count;
  • Use compute() or merge() methods to atomically update counters in ConcurrentHashMap.

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 ConcurrentMap is declared in the PageVisitCounterImpl class and see what happens.

That is, you need to replace this line here with the ConcurrentMap implementation.

java

Main

On this one, with the realization of the usual Map

java

Main

Everything was clear?

Section 2. Chapter 6
We're sorry to hear that something went wrong. What happened?
some-alt