Browse - Programming Tips - How should I setup a flag variable to tell a thread to stop?

Date: 2015sep17
Language: Java

Q.  How should I setup a flag variable to tell a thread to stop?

A.  There are two ways: volatile and AtomicBoolean.
If only the parent thread sets the flag then volatile is ok.
Declare like this:

volatile boolean vbKeepRunning = true;
AtomicBoolean abKeepRunning = new AtomicBoolean(true);