I used to never study for exams without episodes of the simpsons or Top Gear in the background. And I'm happy to saythat it doesn't work.
Just get your head down and study for a certain period of time. Take a break, then do it again.
It's almost like sex. You can't go for just 3 seconds, but you can't keep going on and on for hours on end. Thrust for a few minutes, switch positions, take a breather, say some romantic things, funny things, get ready, thrust again.
That is to say, you shouldn't be saying romantic things and funny things to your book, especially in a public place like a library. And don't have sex with it either. Just study.
For homework, do the same thing but just take it one problem at a time (if it's math or science). If it's English, write a paragraph (if it's writing), read a few pages, and then take a moment to digest it all and take a breather. If you persist in such a manner, your work will be done more efficiently, you won't rack your brain and you won't be too distracted with long and tiring sessions of homeworking and studying.