Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

java - RxJava - Will the observable be garbage collected?

I have some RxJava code similar to this but I wonder if the Subject itself in this case could be garbage collected before my subscription triggers in case the caller doesn't hold a reference on the returned observable and just ignores it.

I am also in a spring environment in which I have a task executor that lets me execute logic on a different thread.

public SingleSubject<...> doStuff(final String someParam) {

    final SingleSubject<...> observable = SingleSubject.create();

    applicationTaskExecutor.execute(() -> {
        try {
            // Usually here some actually logic would be implemented
            try {
                Thread.sleep(15000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            observable.onSuccess(....);

        } catch (Exception e) {
            observable.onError(e);
        }

    });

    observable.observeOn(Schedulers.from(applicationTaskExecutor))
              .subscribe(... do something else when subject above triggers);

    return observable;

}
question from:https://stackoverflow.com/questions/66052388/rxjava-will-the-observable-be-garbage-collected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...