-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathDispatchQueuePool.java
99 lines (90 loc) · 3.42 KB
/
DispatchQueuePool.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package org.telegram.messenger;
import android.os.Build;
import android.os.Looper;
import android.os.SystemClock;
import android.util.SparseIntArray;
import androidx.annotation.UiThread;
import org.telegram.ui.Components.Reactions.HwEmojis;
import java.util.LinkedList;
public class DispatchQueuePool {
private final LinkedList<DispatchQueue> queues = new LinkedList<>();
private final SparseIntArray busyQueuesMap = new SparseIntArray();
private final LinkedList<DispatchQueue> busyQueues = new LinkedList<>();
private int maxCount;
private int createdCount;
private int guid;
private int totalTasksCount;
private boolean cleanupScheduled;
private final Runnable cleanupRunnable = new Runnable() {
@Override
public void run() {
if (!queues.isEmpty()) {
long currentTime = SystemClock.elapsedRealtime();
for (int a = 0, N = queues.size(); a < N; a++) {
DispatchQueue queue = queues.get(a);
if (queue.getLastTaskTime() < currentTime - 30000) {
queue.recycle();
queues.remove(a);
createdCount--;
a--;
N--;
}
}
}
if (!queues.isEmpty() || !busyQueues.isEmpty()) {
AndroidUtilities.runOnUIThread(this, 30000);
cleanupScheduled = true;
} else {
cleanupScheduled = false;
}
}
};
public DispatchQueuePool(int count) {
maxCount = count;
guid = Utilities.random.nextInt();
}
@UiThread
public void execute(Runnable runnable) {
if (Looper.myLooper() != Looper.getMainLooper()) {
AndroidUtilities.runOnUIThread(() -> execute(runnable));
return;
}
DispatchQueue queue;
if (!busyQueues.isEmpty() && (totalTasksCount / 2 <= busyQueues.size() || queues.isEmpty() && createdCount >= maxCount)) {
queue = busyQueues.remove(0);
} else if (queues.isEmpty()) {
queue = new DispatchQueue("DispatchQueuePool" + guid + "_" + Utilities.random.nextInt());
queue.setPriority(Thread.MAX_PRIORITY);
createdCount++;
} else {
queue = queues.remove(0);
}
if (!cleanupScheduled) {
AndroidUtilities.runOnUIThread(cleanupRunnable, 30000);
cleanupScheduled = true;
}
totalTasksCount++;
busyQueues.add(queue);
int count = busyQueuesMap.get(queue.index, 0);
busyQueuesMap.put(queue.index, count + 1);
if (HwEmojis.isHwEnabled()) {
queue.setPriority(Thread.MIN_PRIORITY);
} else if (queue.getPriority() != Thread.MAX_PRIORITY) {
queue.setPriority(Thread.MAX_PRIORITY);
}
queue.postRunnable(() -> {
runnable.run();
AndroidUtilities.runOnUIThread(() -> {
totalTasksCount--;
int remainingTasksCount = busyQueuesMap.get(queue.index) - 1;
if (remainingTasksCount == 0) {
busyQueuesMap.delete(queue.index);
busyQueues.remove(queue);
queues.add(queue);
} else {
busyQueuesMap.put(queue.index, remainingTasksCount);
}
});
});
}
}