1) Devices or people
For both of these, "blocking" is the way to go For people of course this should be more of a request.
2) Other things racing in your mind.
Generally the result of your not tracking all your open loops effectively, the solution is just to jot down in your notebook or task management system whatever the open loop you're thinking about is/needs done about it. It is absolutely key you review this list often enough that you truly trust it.
3) The task itself
In the words of David Allen, author of the life management system Getting Things Done ™, "you're more productive when you're relaxed." In so far as it is the task itself that is distracting you, relaxing and looking at it objectively is a massive help. If needed, take just a second to compare it to a circumstance like being in a war just to make you realise you're actually pretty well off right now and what you're facing isn't so scary.