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:

  1. load variable
  2. compare , branch.
  3. '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

Popular posts from this blog

html - How to custom Bootstrap grid height? -

javascript - pass values from mssql to views in node -

javascript - Understanding ExtJS Framework Syntax -