multithreading - Do you ever use the volatile keyword in Java? -


at work today, came across volatile keyword in java. not being familiar it, found explanation:

java theory , practice: managing volatility

given detail in article explains keyword in question, ever use or ever see case in use keyword in correct manner?

volatile has semantics memory visibility. basically, value of volatile field becomes visible readers (other threads in particular) after write operation completes on it. without volatile, readers see non-updated value.

to answer question: yes, use volatile variable control whether code continues loop. loop tests volatile value , continues if true. condition can set false calling "stop" method. loop sees false , terminates when tests value after stop method completes execution.

the book "java concurrency in practice," highly recommend, gives explanation of volatile. book written same person wrote ibm article referenced in question (in fact, cites book @ bottom of article). use of volatile article calls "pattern 1 status flag."

if want learn more how volatile works under hood, read on the java memory model. if want go beyond level, check out computer architecture book hennessy & patterson , read cache coherence , cache consistency.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -