That's the idea, yes. The boolean should be volatile to make this solution work though.
But you don't even need this flag, because each thread has one already: the interrupted flag. So you could just call a.interrupt();
from thread B, and have your thread A do the following:
public void run() {
while (!Thread.currentThread().isInterrupted()) {
doUninterruptableTasks();
}
}
Note, though, that interrupting a thread blocked in a waiting state will make the blocking method throw an InterruptedException, in order to stop running ASAP. If you don't want that, then use a volatile flag.