Hey, these are the intrusive thoughts, which last for a few seconds, sometimes they do not bother the person much, but sometimes they can be very troublesome and disrupt day to day activities.
You are not alone in this, it can happen to a lot of people and is a very common thing.
Next Steps
First, it's better to start with breathwork. Whenever you get intrusive thought try to take deep inhale at that moment and try to hold your breath, count till 5 in a backward direction.
and I would also like to say, that you should consult a psychiatrist, so that they can evaluate your thoughts and try to understand what could be the reason behind the intrusive thoughts