Mostly while studying CS, or at work.
Joel touched on this towards the end of this article about getting in "the zone" and what knocks you out of it.
Matt Moore also has an article on the matter.
Oh yeah, one more thing. Stop reading Crack Overflow! =)
I find that a game of chess before bedtime helps soothe the mind.
Also, large iced lattes and iced coffees get me through the day.
There's an upcoming book called Pragmatic Thinking and Learning: Refactor Your "Wetware". You can currently grab a beta PDF of it. While it's focused on learning/thinking, I saw one of the presentations the book is based on and I think a lot of it applies to concentration - and it's targetted at programmers.
HTH
Good focus / memory / thinking book:
Mind-performance hacks by Ron Hale-Evans
My favorite tips:
- Work toward hour(s)-long goals, and write down when you start and finish.
- If you have a choice, never work when you're tired.
- If you work in a cubicle, wear headphones.
I found this article useful. It states that concentration itself is a skill, and needs/can be learned just like any other skill. It also lists a few techniques to enhance your concentration.