Use AtomicBoolean in atomic way

In original code it is possible to successfully pass this check with two threads