It's probably just anxiety. Anxiety will do that.
Unless you mean ruminating. Ruminating is when you're thinking about the same things over and over again, focusing on them for long periods of time that it becomes circular thoughts, and if you do it too long you might to start like you're on stuck on a roundabout. It will wear down the body and makes it really hard to get things done.
If it's ruminating, the common way to snap out of it is to have to check in with yourself, remind yourself of the time and where you are, smell something strong, walk around somewhere. It's just to bring you back to the present/reality, because sometimes its easy for us to get lost in our thoughts.
Or ice cream.
We should get ice cream.