Of course not. It has nothing to do with volatile or not, it's just a multithreading nature.
after you called a.halt (), stream a cannot be given the opportunity to continue execution, and your main stream is b.halt (). After that, thread a can still be in place immediately before your println expression.
Just a simplified version will demonstrate:
MAIN THREAD THREAD A while (!stop) a.halt() println(...)
After you called a.halt() , a will still have a chance to continue printing, EVEN stop changes the value of volatile.
To make sure the stream is defined after a certain place, use join ():
WriteThread a = new WriteThread('a'); WriteThread b = new WriteThread('b'); a.start(); b.start();
source share