Skip to content

Commit

Permalink
Try to synchronize SpeedMeter
Browse files Browse the repository at this point in the history
  • Loading branch information
Skidamek committed Nov 26, 2024
1 parent 66ca87d commit fb0bb20
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package pl.skidam.automodpack_loader_core.utils;

import java.util.*;
import java.util.concurrent.ConcurrentSkipListMap;

public class SpeedMeter {

private final DownloadManager downloadManager;
private final TreeMap<Long, Long> bytesDownloadedPerSec = new TreeMap<>();
private final ConcurrentSkipListMap<Long, Long> bytesDownloadedPerSec = new ConcurrentSkipListMap<>();
private static final int MAX_ENTRIES = 5;

public SpeedMeter(DownloadManager downloadManager) {
Expand All @@ -15,7 +15,7 @@ public SpeedMeter(DownloadManager downloadManager) {
/**
* Add new bytes to the current download tally.
*/
public void addDownloadedBytes(long newBytes) {
public synchronized void addDownloadedBytes(long newBytes) {
long bucketedTime = System.currentTimeMillis() / 1000 * 1000;

bytesDownloadedPerSec.merge(bucketedTime, newBytes, Long::sum);
Expand All @@ -28,7 +28,7 @@ public void addDownloadedBytes(long newBytes) {
/**
* Get the download speed in bytes per second.
*/
public long getCurrentSpeedInBytes() {
public synchronized long getCurrentSpeedInBytes() {
long lastTimeBucket = System.currentTimeMillis() / 1000 * 1000 - 1000;

Long value = -1L;
Expand Down

0 comments on commit fb0bb20

Please sign in to comment.