isOn
is a thread local variable, so when one thread modifies it, the other thread won't see the changes. When you use a class with a boolean field, you're presumably giving each thread the same instance of the class, meaning that the threads will see changes made by the other threads. However, you create two instances of Aux
instead of using just one.
If you want it to work on two different instances, try adding the static
modifier (along with the volatile
modifier) to the Boolean
field; otherwise, don't create a second Aux
.