RxJava Schedulers in Java9
Posted By : Dipen Chawla | 27-Apr-2018
Schedulers are one of the fundamental parts
InputOutput — This is a standout amongst the most widely recognized sorts of Schedulers that are utilized. They are for the most part utilized for IO related stuff. For example, arrange demands, record framework activities. IO scheduler is upheld by string pool.
Java Thread pool speaks to a gathering of laborer strings that are sitting tight for the activity and reuse ordinarily.
It begins with making one laborer, which can be re-utilized for different tasks. Obviously, in the event that it can't be re-utilized (in the event of long preparing occupations), it produces another string (or laborer) fo
observable.subscribeOn(Schedulers.io())
Computation — This
observable.subscribeOn(Schedulers.computation())
NewThread — As the name recommends, it produces another string for every dynamic discernible. This can be utilized to offload tedious activity from fundamental string onto other string. Since it just generates another string at whatever point it requires, you have to deal with this since string creation is an exorbitant activity and can have an uncommon impact in versatile condition if the
observable.subscribeOn(Schedulers.newThread())
Single — This
observable.subscribeOn(Schedulers.single())
Immediate — This scheduler(removed from RxJava 2) just begins the undertaking in a blocking path on the present string that is dynamic, slighting which process is at present running. It is accessible as:
observable.subscribeOn(Schedulers.immediate())
Trampoline — This scheduler runs the code on current string. So on the off chance that you have a code running on the primary string, this
Observable.just(1,2,3,4)
.subscribeOn(Schedulers.trampoline())
.subscribe(onNext);
Observable.just( 5,6, 7,8, 9)
.subscribeOn(Schedulers.trampoline())
.subscribe(onNext);
The IScheduler interface is of less significance at the present time than the sorts that execute the interface. The key idea to comprehend is that an IScheduler in Rx is utilized to plan some activity to be performed, either as quickly as time permits or at a given point later on. The usage of the IScheduler characterizes how that activity will be conjured i.e. concurrently by means of a string pool, another string or a message pump, or synchronously on the present string. you will be uncovered a large portion of the usage you will require by means of a static class Scheduler.
Console.WriteLine("Starting on threadId:{0}", Thread.CurrentThread.ManagedThreadId);
var source = Observable.Create<int>(
o =>
{
Console.WriteLine("Invoked on threadId:{0}", Thread.CurrentThread.ManagedThreadId);
o.OnNext(1);
o.OnNext(2);
o.OnNext(3);
o.OnCompleted();
Console.WriteLine("Finished on threadId:{0}",
Thread.CurrentThread.ManagedThreadId);
return Disposable.Empty;
});
source
//.SubscribeOn(Scheduler.ThreadPool)
.Subscribe(
o => Console.WriteLine("Received {1} on threadId:{0}",
Thread.CurrentThread.ManagedThreadId,
o),
() => Console.WriteLine("OnCompleted on threadId:{0}",
Thread.CurrentThread.ManagedThreadId));
Console.WriteLine("Subscribed on threadId:{0}", Thread.CurrentThread.ManagedThreadId)
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Dipen Chawla
Dipen is Java Developer and his keen interest is in Spring, Hibernate, Rest web-services, AngularJS and he is a self motivated person and loves to work in a team.