All Projects → YvesCheung → RxSchedulerSuppress

YvesCheung / RxSchedulerSuppress

Licence: Apache-2.0 license
RxSchedulerSuppress 是用于抑制 RxJava 在同一个线程池内重复调度的工具

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to RxSchedulerSuppress

haxe-concurrent
A haxelib for basic platform-agnostic concurrency support
Stars: ✭ 69 (+130%)
Mutual labels:  scheduler, thread
Swiftcoroutine
Swift coroutines for iOS, macOS and Linux.
Stars: ✭ 690 (+2200%)
Mutual labels:  scheduler, thread
YACLib
Yet Another Concurrency Library
Stars: ✭ 193 (+543.33%)
Mutual labels:  scheduler, thread
Java-Interview-Programs
Core Java Projects with complete source code
Stars: ✭ 48 (+60%)
Mutual labels:  thread
tasks
Package tasks is an easy to use in-process scheduler for recurring tasks in Go
Stars: ✭ 121 (+303.33%)
Mutual labels:  scheduler
ld-scheduler
Schedule Launch Darkly flags on or off
Stars: ✭ 14 (-53.33%)
Mutual labels:  scheduler
celery-beatx
Modern fail-safety scheduler for Celery
Stars: ✭ 46 (+53.33%)
Mutual labels:  scheduler
Jobber
Jobber is lightweight, simple and distributed task scheduler.
Stars: ✭ 25 (-16.67%)
Mutual labels:  scheduler
synchly
Automate database backups with customizable recurring schedules.
Stars: ✭ 27 (-10%)
Mutual labels:  scheduler
Power-Refresh
Schedule refresh of any Excel file using open-source scheduler & refresher written on VBA
Stars: ✭ 55 (+83.33%)
Mutual labels:  scheduler
OPoster
Scheduling Platform for Social Media Networks. Powered by Orienteer
Stars: ✭ 31 (+3.33%)
Mutual labels:  scheduler
transferwisely
Batch process using transfer-wise API to automatically track, detect and book transfers for you at better rates.
Stars: ✭ 20 (-33.33%)
Mutual labels:  scheduler
nnCron
Advanced and very powerful scheduler, scripting tool and automation manager
Stars: ✭ 60 (+100%)
Mutual labels:  scheduler
libopenTIDAL
TIDAL API interface written in ANSI C
Stars: ✭ 17 (-43.33%)
Mutual labels:  thread
cpp-thread-study
C++ 线程库示例及教程
Stars: ✭ 38 (+26.67%)
Mutual labels:  thread
quads
📆 The infrastructure deployment time machine
Stars: ✭ 74 (+146.67%)
Mutual labels:  scheduler
angular-gantt-schedule-timeline-calendar-example
Angular gantt-schedule-timeline-calendar usage example
Stars: ✭ 15 (-50%)
Mutual labels:  scheduler
ComposableAsync
Create, compose and inject asynchronous behaviors in .Net Framework and .Net Core.
Stars: ✭ 28 (-6.67%)
Mutual labels:  thread
java-tutorial
Java 实践代码,多线程,数据结构,算法,设计模式,Spring,RabbitMQ ,RocketMQ
Stars: ✭ 15 (-50%)
Mutual labels:  thread
BotSmartScheduler
Enhance your planning capabilities with this smart bot!
Stars: ✭ 44 (+46.67%)
Mutual labels:  scheduler

RxSchedulerSuppress

RxSchedulerSuppress 是用于抑制 RxJava 在同一个线程池内重复调度的工具。 通过 RxjavaPlugins 替换调度器的实现:如果当前线程已经符合调度器指定的线程池,那么就不再进行线程切换,直接执行。

以Android的主线程调度器为例,对于代码:

Observable.just("Test")
    .observeOn(AndroidSchedulers.mainThread())
    .observeOn(AndroidSchedulers.mainThread())
    .observeOn(AndroidSchedulers.mainThread())
    .observeOn(AndroidSchedulers.mainThread())
    .observeOn(AndroidSchedulers.mainThread())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe((s) -> println(s));

完全等价于以下代码,订阅逻辑会在6个消息循环后再执行:

Handler mainThread = new Handler(Looper.getMainLooper());
mainThread.post(() ->
    mainThread.post(() ->
        mainThread.post(() ->
            mainThread.post(() ->
                mainThread.post(() ->
                    mainThread.post(() -> println("Test"))
                )
            )
        )
    )
);

通过 RxSchedulerSuppress 减少线程切换后,相当于以下代码,若当前为调度器的目标线程,会立即执行:

if(Looper.myLooper() == Looper.mainLooper()){
    println("Test");
} else {
    Handler mainThread = new Handler(Looper.getMainLooper());
    mainThread.post(() -> println("Test"));
}

使用

public class App extends Application {

    public void onCreate() {
        //...
        
        //抑制通过Schedulers.io()从io线程切换到io线程
        //如果当前线程已经是io线程,会立即在当前线程执行
        SchedulerSuppress.SuppressIo();
        //抑制通过Schedulers.compute()从compute线程切换到compute线程
        //如果当前线程已经是io线程,会立即在当前线程执行
        SchedulerSuppress.SuppressCompute();
        
        //or
        //抑制通过Schedulers.io()或者Schedulers.compute()
        //从io线程切换到compute线程,或者从compute线程切换到io线程
        SchedulerSuppress.SuppressBackground();

        //抑制通过AndroidSchedulers.mainThread()从main线程抛到下一次Looper循环
        //如果当前线程已经是main线程,会立即在当前线程执行
        AndroidSchedulerSuppress.SuppressMain();
        
        //以上代码需要在 `RxJavaPlugins.lockDown` 前执行
        RxJavaPlugins.lockDown();
    }
}

效果

抑制 IOIO 的线程切换

Observable
    .create(emitter -> {
        System.out.println("create on " + Thread.currentThread().getName());
        emitter.onNext("Test");
        emitter.onComplete();
    })
    .subscribeOn(Schedulers.io())
    .observeOn(Schedulers.io())
    .map(s -> {
        System.out.println("map on " + Thread.currentThread().getName());
        return s;
    })
    .observeOn(Schedulers.io())
    .flatMapCompletable(s -> {
        System.out.println("flatMap on " + Thread.currentThread().getName());
        return Completable.complete();
    })
    .subscribe();
Before Suppress
After Suppress
  • create on RxCachedThreadScheduler-1
  • map on RxCachedThreadScheduler-2
  • flatMap on RxCachedThreadScheduler-3
  • create on RxCachedThreadScheduler-1
  • map on RxCachedThreadScheduler-1
  • flatMap on RxCachedThreadScheduler-1

抑制 ComputeIO 的线程切换

Observable.timer(1, TimeUnit.MILLISECONDS)
    .map(s -> {
        System.out.println("timer on " + Thread.currentThread().getName());
        return s;
    })
    .observeOn(Schedulers.io())
    .subscribe(s ->
        System.out.println("subscribe on " + Thread.currentThread().getName())
    );
Before Suppress
After Suppress
  • timer on RxComputationThreadPool-1
  • subscribe on RxCachedThreadScheduler-1
  • timer on RxComputationThreadPool-1
  • subscribe on RxComputationThreadPool-1

安装

  1. Add it in your root build.gradle at the end of repositories:
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  1. Add the dependency
dependencies {
    //targeting io.reactivex.schedulers.Schedulers
    implementation 'com.github.YvesCheung.RxSchedulerSuppress:scheduler-suppress:1.0.0'

    //targeting io.reactivex.android.schedulers.AndroidSchedulers
    implementation 'com.github.YvesCheung.RxSchedulerSuppress:scheduler-suppress-android:1.0.0'
}

License

Copyright 2020 Yves Cheung

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].