Skip to content

Commit

Permalink
Update CalculateAverage_EduardoSaverin.java
Browse files Browse the repository at this point in the history
Removed ConcurrentHashMap with Reentrant Lock + HashMap. Since multiple threads causing problems.
  • Loading branch information
EduardoSaverin committed Feb 1, 2024
1 parent fd74fb3 commit fa48079
Showing 1 changed file with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static java.nio.file.StandardOpenOption.READ;

Expand All @@ -35,7 +37,8 @@ public class CalculateAverage_EduardoSaverin {
private static final Unsafe UNSAFE = initUnsafe();
private static final int FNV_32_OFFSET = 0x811c9dc5;
private static final int FNV_32_PRIME = 0x01000193;
private static final Map<String, ResultRow> resultRowMap = new ConcurrentHashMap<>();
private static final Map<String, ResultRow> resultRowMap = new HashMap<>();
private static final Lock lock = new ReentrantLock();

private static Unsafe initUnsafe() {
try {
Expand All @@ -55,20 +58,20 @@ record MapEntry(String key, ResultRow row) {
}

private static final class ResultRow {
private int min;
private int max;
private long sum;
private double min;
private double max;
private double sum;
private int count;

private ResultRow(int v) {
private ResultRow(double v) {
this.min = v;
this.max = v;
this.sum = v;
this.count = 1;
}

public String toString() {
return round(min) + "/" + round((double) (sum) / count) + "/" + round(max);
return round(min) + "/" + round(sum / count) + "/" + round(max);
}

private double round(double value) {
Expand Down Expand Up @@ -144,6 +147,13 @@ static boolean unsafeEquals(final byte[] a, final byte[] b, final short length)
final int baseOffset = Unsafe.ARRAY_BYTE_BASE_OFFSET;

short i = 0;
// Double
for (; i < (length & -8); i += 8) {
if (UNSAFE.getDouble(a, i + baseOffset) != UNSAFE.getDouble(b, i + baseOffset)) {
return false;
}
}

// Long
for (; i < (length & -8); i += 8) {
if (UNSAFE.getLong(a, i + baseOffset) != UNSAFE.getLong(b, i + baseOffset)) {
Expand Down Expand Up @@ -266,6 +276,7 @@ public void run() {
hash = FNV_32_OFFSET;
}
List<MapEntry> all = results.getAll();
lock.lock();
try {
for (MapEntry me : all) {
ResultRow rr;
Expand All @@ -284,6 +295,9 @@ public void run() {
catch (Exception e) {
e.printStackTrace();
}
finally {
lock.unlock();
}
}
}

Expand Down

0 comments on commit fa48079

Please sign in to comment.