I have this service:

```
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    running = true;
    new Thread(){
        @Override
        public void run() {
            super.run();
            while (running) { Thread.sleep(60); ... }
        }
    }.start();
    return super.onStartCommand(intent, flags, startId);
}

```

I saw this example on the Android Bootcamp course on youtube and it worked.
My question is: is that not a dangerous thing to do? because if the service will be stopped while the thread is sleeping its memory could be evicted by the GC and then the thread will try and access an unallocated variable(running)... I suspect it works because it adds + 1 to running's reference counter and that why it will only be evicted when the thread will be over but I would love for someone to confirm this and maybe give a detailed explanation of the process.

Thanks!.