forked from AndroidKnife/RxBus
/
EventThread.java
103 lines (91 loc) · 3.21 KB
/
EventThread.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
100
101
102
103
package com.hwangjr.rxbus.thread;
import android.os.Handler;
import java.util.concurrent.Executor;
import rx.Scheduler;
import rx.android.schedulers.AndroidSchedulers;
import rx.android.schedulers.HandlerScheduler;
import rx.schedulers.Schedulers;
public enum EventThread {
/**
* {@link Scheduler} which will execute actions on the Android UI thread.
*/
MAIN_THREAD,
/**
* Creates and returns a {@link Scheduler} that creates a new {@link Thread} for each unit of work.
* <p/>
* Unhandled errors will be delivered to the scheduler Thread's {@link java.lang.Thread.UncaughtExceptionHandler}.
*/
NEW_THREAD,
/**
* Creates and returns a {@link Scheduler} intended for IO-bound work.
* <p/>
* The implementation is backed by an {@link Executor} thread-pool that will grow as needed.
* <p/>
* This can be used for asynchronously performing blocking IO.
* <p/>
* Do not perform computational work on this scheduler. Use computation() instead.
* <p/>
* Unhandled errors will be delivered to the scheduler Thread's {@link java.lang.Thread.UncaughtExceptionHandler}.
*/
IO,
/**
* Creates and returns a {@link Scheduler} intended for computational work.
* <p/>
* This can be used for event-loops, processing callbacks and other computational work.
* <p/>
* Do not perform IO-bound work on this scheduler. Use io() instead.
* <p/>
* Unhandled errors will be delivered to the scheduler Thread's {@link java.lang.Thread.UncaughtExceptionHandler}.
*/
COMPUTATION,
/**
* Creates and returns a {@link Scheduler} that queues work on the current thread to be executed after the
* current work completes.
*/
TRAMPOLINE,
/**
* Creates and returns a {@link Scheduler} that executes work immediately on the current thread.
*/
IMMEDIATE,
/**
* Converts an {@link Executor} into a new Scheduler instance.
*/
EXECUTOR,
/**
* {@link Scheduler} which uses the provided {@link Handler} to execute actions.
*/
HANDLER;
public static Scheduler getScheduler(EventThread thread) {
Scheduler scheduler;
switch (thread) {
case MAIN_THREAD:
scheduler = AndroidSchedulers.mainThread();
break;
case NEW_THREAD:
scheduler = Schedulers.newThread();
break;
case IO:
scheduler = Schedulers.io();
break;
case COMPUTATION:
scheduler = Schedulers.computation();
break;
case TRAMPOLINE:
scheduler = Schedulers.trampoline();
break;
case IMMEDIATE:
scheduler = Schedulers.immediate();
break;
case EXECUTOR:
scheduler = Schedulers.from(ThreadHandler.DEFAULT.getExecutor());
break;
case HANDLER:
scheduler = HandlerScheduler.from(ThreadHandler.DEFAULT.getHandler());
break;
default:
scheduler = AndroidSchedulers.mainThread();
break;
}
return scheduler;
}
}