-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBaseEventsManager.java
More file actions
68 lines (55 loc) · 2.43 KB
/
BaseEventsManager.java
File metadata and controls
68 lines (55 loc) · 2.43 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package io.split.android.client.events;
import androidx.annotation.NonNull;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import io.split.android.client.utils.logger.Logger;
import io.split.android.engine.scheduler.PausableThreadPoolExecutor;
import io.split.android.engine.scheduler.PausableThreadPoolExecutorImpl;
public abstract class BaseEventsManager implements Runnable {
private final static int QUEUE_CAPACITY = 20;
// Shared thread factory for all instances
private static final ThreadFactory EVENTS_THREAD_FACTORY = createThreadFactory();
protected final ArrayBlockingQueue<SplitInternalEvent> mQueue;
protected final Set<SplitInternalEvent> mTriggered;
private static ThreadFactory createThreadFactory() {
final AtomicInteger threadNumber = new AtomicInteger(1);
return new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, "Split-FactoryEventsManager-" + threadNumber.getAndIncrement());
thread.setDaemon(true);
thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
Logger.e("Unexpected error " + e.getLocalizedMessage());
}
});
return thread;
}
};
}
public BaseEventsManager() {
mQueue = new ArrayBlockingQueue<>(QUEUE_CAPACITY);
mTriggered = Collections.newSetFromMap(new ConcurrentHashMap<>());
launch(EVENTS_THREAD_FACTORY);
}
@Override
public void run() {
// This code was intentionally designed this way
// noinspection InfiniteLoopStatement
while (true) {
triggerEventsWhenAreAvailable();
}
}
private void launch(ThreadFactory threadFactory) {
PausableThreadPoolExecutor mScheduler = PausableThreadPoolExecutorImpl.newSingleThreadExecutor(threadFactory);
mScheduler.submit(this);
mScheduler.resume();
}
protected abstract void triggerEventsWhenAreAvailable();
protected abstract void notifyInternalEvent(SplitInternalEvent event);
}