1. The article
2. To summarize
RxJava
1. Simply start a new thread to execute a logic without transferring data to other threads
RxJava
Observable.create((ObservableOnSubscribe<String>) emitter -> {
// Specify the logic}).subscribeOn(Schedulers.io()).subscribe(); Using Thread directly:new Thread(() -> {
// Specify the logic
}).start();
Copy the code
- In terms of the amount of code, it’s actually simpler to use Thread directly
2. Start thread A to generate data T and transfer data T to thread B
- There are two forms most commonly used
Observable.create((ObservableOnSubscribe<String>) emitter -> {
// Specify the logic
//onNext
emitter.onNext("111");
//onComplete
emitter.onComplete();
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(@NonNull Disposable d) {}@Override
public void onNext(@NonNull String s) {
// Thread A receives data T/"111"
}
@Override
public void onError(@NonNull Throwable e) {}@Override
public void onComplete(a) {}});Copy the code
Observable.just(1)
.map(integer -> "newStr")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Object>() {
@Override
public void accept(Object o) throws Exception {
T/"newStr"}});Copy the code