-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadStatus.java
More file actions
52 lines (45 loc) · 1.49 KB
/
DownloadStatus.java
File metadata and controls
52 lines (45 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package java;
//import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAdder;
// import java.util.concurrent.locks.Lock;
// import java.util.concurrent.locks.ReentrantLock;
public class DownloadStatus {
//private volatile boolean isDone; // adding volatile makes jvm read value from main memory not cache.
//private Lock lock = new ReentrantLock();
//private int totalBytes;
// public int getTotalBytes() {
// return totalBytes;
// }
//private AtomicInteger totalBytes = new AtomicInteger();
private LongAdder totalBytes = new LongAdder();
public int getTotalBytes() {
return totalBytes.intValue();
//return totalBytes.get(); // for Atomic objects.
}
public void incrementTotalBytes(){
totalBytes.increment();
//totalBytes.incrementAndGet(); for Atomic Objects.
}
// using synchronized
// public synchronized void incrementTotalBytes(){
// totalBytes++;
// }
// public boolean isDone() {
// return isDone;
// }
// public void done() {
// isDone = true;
// }
// public synchronized void incrementTotalBytes(){
// // adding synchronized keyword = synchronized(this){totalBytes++;}
// totalBytes++;
// /*-----------for locks------------
// lock.lock();
// try {
// totalBytes++; //non-atmoic operation
// }
// finally{
// lock.unlock();
// }*/
// }
}