阅读此代码AsyncSubscriber.java: 编码器使用AtomicBoolean创建一个Happens Before关系,我想知道:
1_相当于使用同步块吗?
看起来那条线
if (on.get())
dosn确保阻止
try {
final Signal s = inboundSignals.poll(); // We take a signal off the queue
if (!done) { // If we're done, we shouldn't process any more signals, obeying rule 2.8
// Below we simply unpack the `Signal`s and invoke the corresponding methods
if (s instanceof OnNext<?>)
handleOnNext(((OnNext<T>)s).next);
else if (s instanceof OnSubscribe)
handleOnSubscribe(((OnSubscribe)s).subscription);
else if (s instanceof OnError) // We are always able to handle OnError, obeying rule 2.10
handleOnError(((OnError)s).error);
else if (s == OnComplete.Instance) // We are always able to handle OnComplete, obeying rule 2.9
handleOnComplete();
}
}
将在1个时刻执行。
确实当on.get()
返回true时,是什么阻止了另一个线程进入临界区?!
2_它比同步块更有效吗? (假设AtomicBoolean使用Volatile
变量)
这里是代码的一部分:
// We are using this `AtomicBoolean` to make sure that this `Subscriber` doesn't run concurrently with itself,
// obeying rule 2.7 and 2.11
private final AtomicBoolean on = new AtomicBoolean(false);
@SuppressWarnings("unchecked")
@Override public final void run() {
if(on.get()) { // establishes a happens-before relationship with the end of the previous run
try {
final Signal s = inboundSignals.poll(); // We take a signal off the queue
if (!done) { // If we're done, we shouldn't process any more signals, obeying rule 2.8
// Below we simply unpack the `Signal`s and invoke the corresponding methods
if (s instanceof OnNext<?>)
handleOnNext(((OnNext<T>)s).next);
else if (s instanceof OnSubscribe)
handleOnSubscribe(((OnSubscribe)s).subscription);
else if (s instanceof OnError) // We are always able to handle OnError, obeying rule 2.10
handleOnError(((OnError)s).error);
else if (s == OnComplete.Instance) // We are always able to handle OnComplete, obeying rule 2.9
handleOnComplete();
}
} finally {
on.set(false); // establishes a happens-before relationship with the beginning of the next run
if(!inboundSignals.isEmpty()) // If we still have signals to process
tryScheduleToExecute(); // Then we try to schedule ourselves to execute again
}
}
}
// What `signal` does is that it sends signals to the `Subscription` asynchronously
private void signal(final Signal signal) {
if (inboundSignals.offer(signal)) // No need to null-check here as ConcurrentLinkedQueue does this for us
tryScheduleToExecute(); // Then we try to schedule it for execution, if it isn't already
}
// This method makes sure that this `Subscriber` is only executing on one Thread at a time
private final void tryScheduleToExecute() {
if(on.compareAndSet(false, true)) {
try {
executor.execute(this);
} catch(Throwable t) { // If we can't run on the `Executor`, we need to fail gracefully and not violate rule 2.13
if (!done) {
try {
done(); // First of all, this failure is not recoverable, so we need to cancel our subscription
} finally {
inboundSignals.clear(); // We're not going to need these anymore
// This subscription is cancelled by now, but letting the Subscriber become schedulable again means
// that we can drain the inboundSignals queue if anything arrives after clearing
on.set(false);
}
}
}
}
3_安全吗?
4_它是否常用于此目的(创建关系之前发生的事情)?
答案 0 :(得分:1)
是的,写入/读取AtomicBolean会在关系之前发生:
compareAndSet和所有其他读取和更新操作,例如 getAndIncrement具有读写的记忆效应 易变量。
由于您没有发布整个代码,我们也不知道这是如何使用的,因此很难说它是否是线程安全的,但是:
ad 1.它不等同于synchronized块 - 线程不等待
ad 2.是的,它可能更有效率,但compareAndSwap没有义务由volatile
变量支持 - 这是实现的数据。
ad 3.很难说,但run
是一种公共方法的事实暴露了一些错误的可能性,例如,当run
将有两个线程直接调用go
时true
的值。从我的角度来看,最好用run
方法直接做compareAndSwap,但我不了解所有要求,所以这只是一个建议。
ad 4.是的,通常使用AtomicBoolean。