Alternatively, if I can’t focus on a particular task, I’ll work on something else for a while.
I work until I reach a goal or hit a wall and then take a short break by opening up some News or Facebook and read a bit to give my head a rest, and then either get back to where I was stuck or move on to the next step in what I'm working on.
I don't stress out over not being able to solve a problem on the first shot. If I need to look for an answer to a question or solution to a problem I don't sit there and try to figure it out on my own, I start poking around Stackoverflow or do some searching for what I need to know first.
I almost always find what I need, and in those rare cases I cannot I will ask a question wherever it's most appropriate and generally someone points me in the direction I need to go. In the meantime I'll choose something else I need to work on, generally something I know I can easily make some progress on.
I've found there is almost nothing I need to do that hasn't been done and documented somewhere and it's a waste of time to try and "resolve" things that already have a solid solution and I learn something every time I do that.
As long as I'm learning or making progress it's easy to stay focused and be productive. Those short breaks I take when I hit a wall give me a chance to come back to a problem and look at it with a fresh perspective. This way I never feel like it's impossible to overcome, or, worse yet, I'm not up to the challenge.