volatile !volatile boolean isStopped = false;doInBackground() { while (!isStopped) { // do your work. } cancel() { isStopped = true; } } Note the key point is : in multithread occasion, var may not be seen immediately by other thread when modified, but if decorated by volatile, and when there is only one thread do write(modify the var), the other read thread can see it immediately.It is related to system cache.
BTW : AtomicBoolean, or synchronized can also solve the problem.
// do your work.
} cancel() {
isStopped = true;
}
}
Note the key point is : in multithread occasion, var may not be seen immediately by other thread when modified,
but if decorated by volatile, and when there is only one thread do write(modify the var), the other read thread
can see it immediately.It is related to system cache.