Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
:D
Cool, correct synchronizatoin
In totalVisits long
volatile
is excess. Becausesynchronized
provide belowvolatile
provide onlywhen you sync access to variable, you can do not write
volatile
.Also sync access to concurrent hash map is excess.
Your hash map have race condition.
T1: get value from hash map for username "Steve"
T2: get value from hash map for username "Steve" too
T1: set counter value for "Steve" to 1
T2: set counter value for "Steve" to 1
But this should be 2 and addings atomic long here not solve this problem. You should sync access to hash map or use concurrent hash map with atomic operations like 'compute'
This is incorrect synchornization.
totalVisits
also must be synchronized on same monitor thatvisit
method, also this rule must be applied tovisitsBy
methodExplain