You are executing a second thread that sets the code
instance variable to 7
without any synchronization.
(您正在执行第二个线程,该线程将code
实例变量设置为7
而没有任何同步。)
And you access the code
variable from your main thread, again without any synchronization.
(而且,您可以再次从主线程访问code
变量,而无需进行任何同步。)
This means that the main thread may see the state of the code
variable prior to the other thread setting it to 7
(which means it would print 9
until it observes the change made by the other thread), or it may not (which means it would only print 7
s).
(这意味着主线程可能会在另一个线程将其设置为7
之前先看到code
变量的状态(这意味着它会打印9
直到观察到另一个线程所做的更改),否则可能不会(这意味着它只会打印7
秒)。)
It all depends on which thread will run first. (这完全取决于哪个线程将首先运行。)
Without synchronization you have no reason to expect one outcome over the other.
(没有同步,您没有理由期望一个结果超过另一个。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…