diff --git a/library/src/main/java/com/liulishuo/filedownloader/download/DownloadStatusCallback.java b/library/src/main/java/com/liulishuo/filedownloader/download/DownloadStatusCallback.java index 617165d7..4fa68d28 100644 --- a/library/src/main/java/com/liulishuo/filedownloader/download/DownloadStatusCallback.java +++ b/library/src/main/java/com/liulishuo/filedownloader/download/DownloadStatusCallback.java @@ -37,6 +37,7 @@ import java.io.File; import java.io.IOException; +import java.util.concurrent.atomic.AtomicLong; import static com.liulishuo.filedownloader.download.FetchDataTask.BUFFER_SIZE; import static com.liulishuo.filedownloader.model.FileDownloadModel.TOTAL_VALUE_IN_CHUNKED_RESOURCE; @@ -121,14 +122,12 @@ void onMultiConnection() { private volatile long lastCallbackTimestamp = 0; - private volatile long callbackIncreaseBuffer = 0; - private final Object increaseLock = new Object(); + private final AtomicLong callbackIncreaseBuffer = new AtomicLong(); void onProgress(long increaseBytes) { - synchronized (increaseLock) { - this.callbackIncreaseBuffer += increaseBytes; - model.setSoFar(model.getSoFar() + increaseBytes); - } + + callbackIncreaseBuffer.addAndGet(increaseBytes); + model.increaseSoFar(increaseBytes); model.setStatus(FileDownloadStatus.progress); @@ -146,10 +145,8 @@ void onProgress(long increaseBytes) { } void onRetry(Exception exception, int remainRetryTimes, long invalidIncreaseBytes) { - synchronized (increaseLock) { - this.callbackIncreaseBuffer = 0; - model.setSoFar(model.getSoFar() - invalidIncreaseBytes); - } + this.callbackIncreaseBuffer.set(0); + model.increaseSoFar(-invalidIncreaseBytes); if (handler == null) { // direct @@ -372,9 +369,7 @@ private void handleProgress(final long now, if (isNeedCallbackToUser) { lastCallbackTimestamp = now; onStatusChanged(FileDownloadStatus.progress); - synchronized (increaseLock) { - callbackIncreaseBuffer = 0; - } + callbackIncreaseBuffer.set(0); } } @@ -460,7 +455,7 @@ private boolean isNeedCallbackToUser(final long now) { final long callbackTimeDelta = now - lastCallbackTimestamp; - return (callbackMinIntervalBytes != NO_ANY_PROGRESS_CALLBACK && callbackIncreaseBuffer >= callbackMinIntervalBytes) + return (callbackMinIntervalBytes != NO_ANY_PROGRESS_CALLBACK && callbackIncreaseBuffer.get() >= callbackMinIntervalBytes) && (callbackTimeDelta >= callbackProgressMinInterval); } diff --git a/library/src/main/java/com/liulishuo/filedownloader/model/FileDownloadModel.java b/library/src/main/java/com/liulishuo/filedownloader/model/FileDownloadModel.java index cd630dcd..70921c5a 100644 --- a/library/src/main/java/com/liulishuo/filedownloader/model/FileDownloadModel.java +++ b/library/src/main/java/com/liulishuo/filedownloader/model/FileDownloadModel.java @@ -24,6 +24,7 @@ import com.liulishuo.filedownloader.util.FileDownloadUtils; import java.io.File; +import java.util.concurrent.atomic.AtomicLong; /** * The model of the downloading task will be used in the filedownloader database. @@ -58,7 +59,7 @@ public class FileDownloadModel implements Parcelable { private byte status; public final static String STATUS = "status"; - private long soFar; + private final AtomicLong soFar; private long total; public final static String SOFAR = "sofar"; @@ -92,7 +93,11 @@ public void setStatus(byte status) { } public void setSoFar(long soFar) { - this.soFar = soFar; + this.soFar.set(soFar); + } + + public void increaseSoFar(long increaseBytes){ + this.soFar.addAndGet(increaseBytes); } public void setTotal(long total) { @@ -145,7 +150,7 @@ public byte getStatus() { } public long getSoFar() { - return soFar; + return soFar.get(); } public long getTotal() { @@ -274,7 +279,7 @@ public void writeToParcel(Parcel dest, int flags) { dest.writeByte(this.pathAsDirectory ? (byte) 1 : (byte) 0); dest.writeString(this.filename); dest.writeByte(this.status); - dest.writeLong(this.soFar); + dest.writeLong(this.soFar.get()); dest.writeLong(this.total); dest.writeString(this.errMsg); dest.writeString(this.eTag); @@ -283,6 +288,7 @@ public void writeToParcel(Parcel dest, int flags) { } public FileDownloadModel() { + this.soFar = new AtomicLong(); } protected FileDownloadModel(Parcel in) { @@ -292,7 +298,7 @@ protected FileDownloadModel(Parcel in) { this.pathAsDirectory = in.readByte() != 0; this.filename = in.readString(); this.status = in.readByte(); - this.soFar = in.readLong(); + this.soFar = new AtomicLong(in.readLong()); this.total = in.readLong(); this.errMsg = in.readString(); this.eTag = in.readString();