【发布时间】:2020-01-28 10:03:02
【问题描述】:
我有一个用 synchronized(this) 包裹的块,我在调试模式和日志中都看到 2 个线程同时进入此部分。
public void dispatch(Event.Builder eventBuilder) {
synchronized (this) {
index++;
getLogger().d(TAG, "race condition line A - The index is " + index);
try {
Event event = eventBuilder.build();
getLogger().d(TAG, "race condition line B - The index is " + index);
mDispatcher.dispatch(event);
} catch (InstantiationWithoutBuilderException e) {
// Dev time Exception. Should be caught by Developer
throw e;
} catch (StateMachineException e) {
if (!e.wasWrittenToErrorHistory()) {
printError(new ExceptionHistoryElement(mState, eventBuilder.getTemporaryEventWithTypeForException(), e));
}
} catch (Exception e) {
printError(new ExceptionHistoryElement(mState, eventBuilder.getTemporaryEventWithTypeForException(), e));
}
getLogger().d(TAG, "race condition line C - The index is " + index);
}
}
日志:
race condition line A - The index is 1
race condition line B - The index is 1
race condition line A - The index is 2
race condition line B - The index is 2
race condition line C - The index is 2
race condition line A - The index is 3
race condition line B - The index is 3
race condition line C - The index is 3
race condition line C - The index is 3
race condition line A - The index is 4
race condition line B - The index is 4
race condition line C - The index is 4
race condition line A - The index is 5
race condition line B - The index is 5
race condition line C - The index is 5
如您所见,每次进入同步块时,我都会增加数据成员索引。 它应该为每个索引打印 3 个日志行, 但正如您在日志中看到的那样,索引 1 打印了两次,索引 3 打印了 4 次。
谢谢
更新: 事实证明它正在发生,因为同一个线程多次进入此方法。同步块仅适用于不同线程之间。这是如何在同步代码中发生的,这是新的谜。
【问题讨论】:
-
您在运行时是否有多个
StateMachine实例? -
@Michael 一个人对另一个人说:“呃,你。好像我没有足够的能力来抗衡。”
-
我用文本代码编辑并替换了屏幕截图
标签: java android synchronization race-condition synchronized