I would say "While sleeping or praying, thousands of words roam my mind."
'Knock' doesn't work. I guess you could say 'thousands of words knock about/knock around in my mind'.
However, they don't convey quite the same sense. With 'roam', there's an image of many words wandering around slowly with some occasionally catching your attention, while with 'knock about/around' there's a sense that the words are in conflict, jostling each other for your attention.
Student or Learner