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

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -