Well, same as the previous task. I really can't find it. (which is worrying me quite a bit for my future in the programing field I must say...).
I saw the post of Andrew, I saw he says to look for the concurrentHashMap code to see clues and I saw another guy resolved it with just that. (which worrying me even more). So I looked at the code of the concurrentHashedMap, understood almost nothing and only understood that they don't use synchronized at all....
If someone was kind enough to show me something more specific It would be awesome.
Thanks.
(the objects in the synchronized (blab) are just stupids randoms trials I've done)
package com.codegym.task.task26.task2609;
/*
Distributing items into baskets with their own lock
*/
public class Solution {
private static final int LOCK_COUNT = 12;
private final Node[] buckets;
private final Object[] locks;
static class Node {
public Node next;
public Object key;
public Object value;
}
public Solution(int bucketCount) {
buckets = new Node[bucketCount];
locks = new Object[LOCK_COUNT];
for (int i = 0; i < LOCK_COUNT; i++) {
locks[i] = new Object();
}
}
private final int hash(Object key) {
return Math.abs(key.hashCode() % buckets.length);
}
public Object get(Object key) {
int hash = hash(key);
synchronized (locks[hash]) {
for (Node m = buckets[hash]; m != null; m = m.next) {
if (m.key.equals(key)) return m.value;
}
}
return null;
}
public void clear() {
for (int i = 0; i < buckets.length; i++) {
synchronized (locks[i]) {
buckets[i] = null;
}
}
}
public static void main(String[] args) {
}
}