Your problem seems to be a lack of focus and its resulting compulsion. If you want to practice focus you can just try to pay attention to your breath and increase the duration of that exercise over time, or practice calculations or other short-term memory tasks in your head.
stop daydreaming
Overthinking can be a habit that you do not realize, for example when you suddenly daydream.
Begin to pay attention to your way of thinking and realize that your mind is empty when daydreaming, so you can quickly become aware and insolent to think over something.
In addition to daydreaming, repeating an event in the mind can also drag you into overthinking. When that happens, you'll be worried about something. It's a sign that your mind is not being productive.