在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ 調(diào)度器 Scheduler
調(diào)度器 Scheduler
Empty/Never/Throw
Replay
這個(gè)頁(yè)面展示了創(chuàng)建Observable的各種方法。
ObserveOn
ReactiveX
TimeInterval
Window
本頁(yè)展示的操作符用于對(duì)整個(gè)序列執(zhí)行算法操作或其它操作,由于這些操作必須等待數(shù)據(jù)發(fā)射完成(通常也必須緩存這些數(shù)據(jù)),它們對(duì)于非常長(zhǎng)
IgnoreElements
Distinct
Last
Start
And/Then/When
Switch
創(chuàng)建操作
Materialize/Dematerialize
CombineLatest
Catch
實(shí)現(xiàn)自己的操作符
StringObservable
Map
ConnectableObservable
Using
Take
BlockingObservable
TakeLast
Defer
RxJavaSchedulersHook
First
FlatMap
這個(gè)頁(yè)面的操作符可用于根據(jù)條件發(fā)射或變換Observables,或者對(duì)它們做布爾運(yùn)算:
Do
Repeat
Serialize
這個(gè)頁(yè)面展示的操作符可用于過(guò)濾和選擇Observable發(fā)射的數(shù)據(jù)序列。
這個(gè)頁(yè)面列出了很多用于Observable的輔助操作符
Single
Retry
從錯(cuò)誤中恢復(fù)的技術(shù)
Sample
Merge
算術(shù)和聚合操作
Range
Timestamp
RxJava Issues
From
Subscribe
Subject
Delay
Skip
SubscribeOn
Filter
按字母順序排列的全部操作符列表
Timeout
Scan
onError
Zip
RxJava文檔和教程
Publish
ElementAt
第一個(gè)例子
SkipLast
Just
Timer
Debounce
GroupBy
條件和布爾操作
這個(gè)頁(yè)面展示了可用于對(duì)Observable發(fā)射的數(shù)據(jù)執(zhí)行變換操作的各種操作符。
Introduction
rxjava-async
介紹響應(yīng)式編程
這個(gè)頁(yè)面展示的操作符可用于組合多個(gè)Observables。
ReactiveX
Connect
操作符分類
StartWith
Interval
Join
To
Buffer
RefCount
介紹
Observable

調(diào)度器 Scheduler

如果你想給Observable操作符鏈添加多線程功能,你可以指定操作符(或者特定的Observable)在特定的調(diào)度器(Scheduler)上執(zhí)行。

某些ReactiveX的Observable操作符有一些變體,它們可以接受一個(gè)Scheduler參數(shù)。這個(gè)參數(shù)指定操作符將它們的部分或全部任務(wù)放在一個(gè)特定的調(diào)度器上執(zhí)行。

使用ObserveOn和SubscribeOn操作符,你可以讓Observable在一個(gè)特定的調(diào)度器上執(zhí)行,ObserveOn指示一個(gè)Observable在一個(gè)特定的調(diào)度器上調(diào)用觀察者的onNext, onError和onCompleted方法,SubscribeOn更進(jìn)一步,它指示Observable將全部的處理過(guò)程(包括發(fā)射數(shù)據(jù)和通知)放在特定的調(diào)度器上執(zhí)行。

RxJava示例

調(diào)度器的種類

下表展示了RxJava中可用的調(diào)度器種類:

調(diào)度器類型 效果
Schedulers.computation(?) 用于計(jì)算任務(wù),如事件循環(huán)或和回調(diào)處理,不要用于IO操作(IO操作請(qǐng)使用Schedulers.io());默認(rèn)線程數(shù)等于處理器的數(shù)量
Schedulers.from(executor) 使用指定的Executor作為調(diào)度器
Schedulers.immediate(?) 在當(dāng)前線程立即開始執(zhí)行任務(wù)
Schedulers.io(?) 用于IO密集型任務(wù),如異步阻塞IO操作,這個(gè)調(diào)度器的線程池會(huì)根據(jù)需要增長(zhǎng);對(duì)于普通的計(jì)算任務(wù),請(qǐng)使用Schedulers.computation();Schedulers.io(?)默認(rèn)是一個(gè)CachedThreadScheduler,很像一個(gè)有線程緩存的新線程調(diào)度器
Schedulers.newThread(?) 為每個(gè)任務(wù)創(chuàng)建一個(gè)新線程
Schedulers.trampoline(?) 當(dāng)其它排隊(duì)的任務(wù)完成后,在當(dāng)前線程排隊(duì)開始執(zhí)行

默認(rèn)調(diào)度器

在RxJava中,某些Observable操作符的變體允許你設(shè)置用于操作執(zhí)行的調(diào)度器,其它的則不在任何特定的調(diào)度器上執(zhí)行,或者在一個(gè)指定的默認(rèn)調(diào)度器上執(zhí)行。下面的表格個(gè)列出了一些操作符的默認(rèn)調(diào)度器:

操作符 調(diào)度器
buffer(timespan) computation
buffer(timespan,?count) computation
buffer(timespan,?timeshift) computation
debounce(timeout,?unit) computation
delay(delay,?unit) computation
delaySubscription(delay,?unit) computation
interval computation
repeat trampoline
replay(time,?unit) computation
replay(buffersize,?time,?unit) computation
replay(selector,?time,?unit) computation
replay(selector,?buffersize,?time,?unit) computation
retry trampoline
sample(period,?unit) computation
skip(time,?unit) computation
skipLast(time,?unit) computation
take(time,?unit) computation
takeLast(time,?unit) computation
takeLast(count,?time,?unit) computation
takeLastBuffer(time,?unit) computation
takeLastBuffer(count,?time,?unit) computation
throttleFirst computation
throttleLast computation
throttleWithTimeout computation
timeInterval immediate
timeout(timeoutSelector) immediate
timeout(firstTimeoutSelector,?timeoutSelector) immediate
timeout(timeoutSelector,?other) immediate
timeout(timeout,?timeUnit) computation
timeout(firstTimeoutSelector,?timeoutSelector,?other) immediate
timeout(timeout,?timeUnit,?other) computation
timer computation
timestamp immediate
window(timespan) computation
window(timespan,?count) computation
window(timespan,?timeshift) computation

使用調(diào)度器

除了將這些調(diào)度器傳遞給RxJava的Observable操作符,你也可以用它們調(diào)度你自己的任務(wù)。下面的示例展示了Scheduler.Worker的用法:


worker = Schedulers.newThread().createWorker();
worker.schedule(new Action0() {

    @Override
    public void call() {
        yourWork();
    }

});
// some time later...
worker.unsubscribe();

遞歸調(diào)度器

要調(diào)度遞歸的方法調(diào)用,你可以使用schedule,然后再用schedule(this),示例:


worker = Schedulers.newThread().createWorker();
worker.schedule(new Action0() {

    @Override
    public void call() {
        yourWork();
        // recurse until unsubscribed (schedule will do nothing if unsubscribed)
        worker.schedule(this);
    }

});
// some time later...
worker.unsubscribe();

檢查或設(shè)置取消訂閱狀態(tài)

Worker類的對(duì)象實(shí)現(xiàn)了Subscription接口,使用它的isUnsubscribed和unsubscribe方法,所以你可以在訂閱取消時(shí)停止任務(wù),或者從正在調(diào)度的任務(wù)內(nèi)部取消訂閱,示例:


Worker worker = Schedulers.newThread().createWorker();
Subscription mySubscription = worker.schedule(new Action0() {

    @Override
    public void call() {
        while(!worker.isUnsubscribed()) {
            status = yourWork();
            if(QUIT == status) { worker.unsubscribe(); }
        }
    }

});

Worker同時(shí)是Subscription,因此你可以(通常也應(yīng)該)調(diào)用它的unsubscribe方法通知可以掛起任務(wù)和釋放資源了。

延時(shí)和周期調(diào)度器

你可以使用schedule(action,delayTime,timeUnit)在指定的調(diào)度器上延時(shí)執(zhí)行你的任務(wù),下面例子中的任務(wù)將在500毫秒之后開始執(zhí)行:

someScheduler.schedule(someAction, 500, TimeUnit.MILLISECONDS);

使用另一個(gè)版本的schedule,schedulePeriodically(action,initialDelay,period,timeUnit)方法讓你可以安排一個(gè)定期執(zhí)行的任務(wù),下面例子的任務(wù)將在500毫秒之后執(zhí)行,然后每250毫秒執(zhí)行一次:

someScheduler.schedulePeriodically(someAction, 500, 250, TimeUnit.MILLISECONDS);

測(cè)試調(diào)度器

TestScheduler讓你可以對(duì)調(diào)度器的時(shí)鐘表現(xiàn)進(jìn)行手動(dòng)微調(diào)。這對(duì)依賴精確時(shí)間安排的任務(wù)的測(cè)試很有用處。這個(gè)調(diào)度器有三個(gè)額外的方法:

  • advanceTimeTo(time,unit) 向前波動(dòng)調(diào)度器的時(shí)鐘到一個(gè)指定的時(shí)間點(diǎn)
  • advanceTimeBy(time,unit) 將調(diào)度器的時(shí)鐘向前撥動(dòng)一個(gè)指定的時(shí)間段
  • triggerActions(?) 開始執(zhí)行任何計(jì)劃中的但是未啟動(dòng)的任務(wù),如果它們的計(jì)劃時(shí)間等于或者早于調(diào)度器時(shí)鐘的當(dāng)前時(shí)間