Skip to content

Commit

Permalink
prefs: using the AtomicLong instead of lock to make better efficiency…
Browse files Browse the repository at this point in the history
… on increase progressing
  • Loading branch information
Jacksgong committed Aug 19, 2017
1 parent d2936a4 commit 0c3abcb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -372,9 +369,7 @@ private void handleProgress(final long now,
if (isNeedCallbackToUser) {
lastCallbackTimestamp = now;
onStatusChanged(FileDownloadStatus.progress);
synchronized (increaseLock) {
callbackIncreaseBuffer = 0;
}
callbackIncreaseBuffer.set(0);
}
}

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -145,7 +150,7 @@ public byte getStatus() {
}

public long getSoFar() {
return soFar;
return soFar.get();
}

public long getTotal() {
Expand Down Expand Up @@ -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);
Expand All @@ -283,6 +288,7 @@ public void writeToParcel(Parcel dest, int flags) {
}

public FileDownloadModel() {
this.soFar = new AtomicLong();
}

protected FileDownloadModel(Parcel in) {
Expand All @@ -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();
Expand Down

0 comments on commit 0c3abcb

Please sign in to comment.