Java -class library -based complicated concurrent programming technical principles based on the RXJAVA framework
Java -class library response concurrent programming technical principles based on the RXJAVA framework
Overview
Response concurrent programming is an event -based programming paradigm. It implements concurrent operation by dividing the task into a discrete event flow.RXJAVA is a popular response programming framework that provides rich tools and operators, making the response concurrent programming in the Java class library more simple and efficient.
This article will introduce the technical principles of the Java class library -based concurrent programming of the RXJAVA framework, and provide some Java code examples.
1. Observer mode
Rxjava realizes responsive programming based on the observer mode.In this mode, there is an observed object (observed) and a group of observer objects.When the object observed changes, all observers are notified.In RXJAVA, the observed object is called "observable", and the observer object is called "Observer".
The following is a simple example that shows how to create an Observable and subscribe to an Observer:
Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
emitter.onNext("Hello");
emitter.onNext("RxJava");
emitter.onComplete();
}
});
Observer<String> observer = new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
// Call when subscribing
}
@Override
public void onNext(String value) {
// Call when receiving the data
System.out.println(value);
}
@Override
public void onError(Throwable e) {
// Call when an error occurs
}
@Override
public void onComplete() {
// Call when finished
}
};
observable.subscribe(observer);
2. Schedulers (schedulers)
In concurrent programming, the execution of tasks usually needs to consider the problem of thread scheduling.Rxjava provides a scheduler to easily control the execution thread of the task.Through the scheduler, we can specify the task to run in different threads, such as IO threads, computing threads, or UI threads of Android.
The following is an example of using a scheduler:
Observable.just("Hello")
.subscribeon (schedulers.io ()) // Specify the task in the IO thread execute
.observeon (AndroidSchedulers.maintHread ()) // Specify the results processing in the UI thread
.subscribe(new Consumer<String>() {
@Override
public void accept(String value) {
System.out.println(value);
}
});
In the above examples, the task will be executed in the IO thread, and then the result will be processed in the UI thread.
3. Conversion and filter operator
RXJAVA provides a wealth of operators for conversion and filtering of event streams.
For example, the "MAP" operator can be used to convert one event with another event:
Observable.just("Hello")
.map(new Function<String, String>() {
@Override
public String apply(String value) {
return value + " RxJava";
}
})
.subscribe(new Consumer<String>() {
@Override
public void accept(String value) {
System.out.println(value);
}
});
In the above example, the "MAP" operator converts "Hello" to "Hello Rxjava".
In addition to the conversion operator, RXJAVA also provides a series of filtering operators, such as "Filter", "Take" and "SKIP" to filter and screen the event stream.
4. Abnormal treatment
In concurrent programming, abnormal treatment is an important issue.Rxjava provides multiple ways to deal with errors and abnormalities.
Observable.just(1, 2, 3, 4, 5)
.map(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer value) {
if (value == 3) {
throw new RuntimeException("Error!");
}
return value;
}
})
.onErrorResumeNext(new Function<Throwable, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Throwable throwable) throws Exception {
// When you encounter an error, return a new Observable
return Observable.just(6, 7, 8, 9, 10);
}
})
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer value) {
System.out.println(value);
}
});
In the above example, an exception encountered an exception in the "MAP" operator, but by using the "Onerrorresumenext" operator, we can return a new Observable and continue to execute.
Summarize
This article introduces the technical principles of Java -class library -based concurrent programming of Java -class libraries based on the RXJAVA framework.By using the RXJAVA observer mode, scheduling, conversion and filtering operator, and abnormal processing, we can more conveniently implement the response concurrent programming.Rxjava provides powerful tools to make concurrent programming simpler and efficient.
The above is a knowledge article about the principles of programming technology principles based on the Java class library -based library -based library -based library -based library.I hope to provide some help to readers to understand and apply RXJAVA.