multithreading - Is the !boolean a atomic operation in java? -
can code below can work in multithreading? !boolean atomic operation in java?
volatile boolean flag = true; if (!flag){ }
is
!boolean
a[n] atomic operation in java?
no. there 3 operations here:
- load variable
- compare , branch.
- 'do something'.
a thread-switch can occur between 1 , 2, or 2 , 3, , value of underlying boolean can change @ either time, or during 3, or after it.
so if want sure 'do something' happens if flag
simultaneously false, have synchronize, or use semaphore.
Comments
Post a Comment