ref:https://github.com/ReactiveX/RxAndroid/issues/420
在这种情况下:
//pseudo-code
websocket(Callback(data){
//websocket very frequent data in no main thread
Observable.just(data)
.observeOn(Schedulers.computation())
.subscribe(data -> {
//computation thread
map2Obj(data);
});
});
//computation
void map2Obj(data){
//....
then change to main thread
}
------------------打击是执行器服务实现模型-----------------------------------
在这种情况下:
//pseudo-code
static ExecutorService mExecutorService;
static {
mExecutorService = Executors.newFixedThreadPool(8);
}
websocket(Callback(data){
//websocket very frequent data in no main thread。change to other compute thread to prevent block "the thread of getting data"。in run() execute map2Obj(data)
mExecutorService.execute(new NewFixThread(str));
});
//computation
void map2Obj(data){
//....
then change to main thread
}
RxJava更好还是java Executors?为什么?
感谢!!!!!
它会根据需要随时切换线程。
仅此而已。
示例代码将不断将数据从主线程移动到另一个线程。这将导致每次几微秒的性能损失,具体取决于必须创建线程的方式和时间。
完全按照你的吩咐。