RxJava Completable 的 Emitter.onComplete 是 happens-before Observer 的回调吗?
Is RxJava Completable's Emitter.onComplete happens-before Observer's callback?
给定以下代码,是否保证 System.out.println(v)
将打印 1?如果我将 io 和计算调度程序更改为其他调度程序会怎样?
我查看了computation scheduler的源码,好像使用了executor的submit方法,根据文档,submit是happens-before实际runnable的执行,所以我认为在这种情况下,这种happens-before关系是有保证的,但这是否适用于其他调度程序?
import io.reactivex.Completable;
import io.reactivex.schedulers.Schedulers;
public class Test {
static int v = 0;
public static void main(String[] args){
Completable.create(e -> {v = 1; e.onComplete();})
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.subscribe(() -> System.out.println(v));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
此外,如果我在 Completable#create
之前将 1 分配给 v,此更改是否对 Completable 的主体可见?
Given the following code, is it guaranteed that System.out.println(v) will print 1?
是的。
但是,如果您调换了订单,则无法保证:
Completable.create(e -> {e.onComplete(); v = 1;})
What if I change the io and computation schedulers to other schedulers?
所有标准调度程序都有此保证。
but is this apply to other schedulers?
任何异步调度程序都应该提供这种先行发生关系,并且由于底层 ExecutorService 而保证标准的调度程序。
if I assign 1 to v before Completable#create, is this change visible to Completable's body?
subscribeOn
也建立了先行关系,因此在订阅时,v
被提交并且创建的主体将看到该值。
给定以下代码,是否保证 System.out.println(v)
将打印 1?如果我将 io 和计算调度程序更改为其他调度程序会怎样?
我查看了computation scheduler的源码,好像使用了executor的submit方法,根据文档,submit是happens-before实际runnable的执行,所以我认为在这种情况下,这种happens-before关系是有保证的,但这是否适用于其他调度程序?
import io.reactivex.Completable;
import io.reactivex.schedulers.Schedulers;
public class Test {
static int v = 0;
public static void main(String[] args){
Completable.create(e -> {v = 1; e.onComplete();})
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.subscribe(() -> System.out.println(v));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
此外,如果我在 Completable#create
之前将 1 分配给 v,此更改是否对 Completable 的主体可见?
Given the following code, is it guaranteed that System.out.println(v) will print 1?
是的。
但是,如果您调换了订单,则无法保证:
Completable.create(e -> {e.onComplete(); v = 1;})
What if I change the io and computation schedulers to other schedulers?
所有标准调度程序都有此保证。
but is this apply to other schedulers?
任何异步调度程序都应该提供这种先行发生关系,并且由于底层 ExecutorService 而保证标准的调度程序。
if I assign 1 to v before Completable#create, is this change visible to Completable's body?
subscribeOn
也建立了先行关系,因此在订阅时,v
被提交并且创建的主体将看到该值。