I really hate it when I am reading a good book then later my mind starts to wander and it's not because I am not interested in the subject. I think it is just too easy to get distracted. Lately I have been trying a new trick on staying focused when reading or writing code.
About a month ago I read a book called Pragmatic Thinking and Learning: Refactor Your Wetware by Andy Hunt, one of the co-writers of The Pragmatic Programmer. In this book he had mentioned some really neat things about the human mind. Such like a computer, our mind is freaking asynchronous. I later had noticed that this is very true. Ever had an idea just jump out at you from no where to solve a problem? That is because our mind runs background jobs to retrieve information. This is what can cause your mind to wander when you want to stay focused. For an example, the dishes are dirty and you see that. When you start to read that book or write that code the idea then pops into your head that you need to clean the dishes and distracts you from the task at hand. This stuff happens to me a lot when being at home and trying to be productive.
So the next time you need to stay focused on the task at hand, try something like this. First, remove as much distractions as possible around you, if you are writing code then close your email, browser(if not web), chat and etc. When listening to music, make a playlist where you are familiar with the music. New songs could distract from your task to write the song down before the next one plays.
I will keep a notebook next to me with a pen. When your mind starts to wander, write on the back sheet of the notebook that exact thought then continue, when your done with your task you can take care of your distractions then because you probably don't need to take care of it right now. On the front side of the notebook, take notes of your current task.
I find that this helps when I read. After each paragraph write a one sentence(or more) summary about what you have read. Writing the short summary helps to understand and learn the content. A site called Good Reads is a great way to track progress to keep moving forward. In reality when most people just read a book they only remember something like 10%? I can relate, I have read some books and not remember anything.
Lastly, I won't use a computer when writing these thoughts down because it doesn't feel permanent. Writing with pen on paper really takes your thoughts out of your mind.