I don't think you want to iterate on all messages each time you register a new message. A thread can be huge.

This iis a good case for a directory instead (object or Map I don't mind).