On 09/10/12 15:27, Artem Ananiev wrote:
** This is safe because a thread only ever writes its own value to
flushThread so even if it reads a stale value that value will either be
null or some other thread - either way it is not the current thread so
it proceeds with the main logic.

The "flushThread" field is not volatile, so we can't check its value
outside of synchronized blocks.

In this particular case you can do that, and in the above quote David explains why.

In other words: you only want to check whether the flushThread refers to the current thread or not. If it's been actually set by the current thread, then this thread must see the correct value w/o any synchronization needed. Otherwise, (if it's null or set by another thread,) your code will see a value that doesn't refer to the current thread, and this is exactly what you wanted to check.

So I agree with David, this test needs no synchronization.

--
best regards,
Anthony

Reply via email to