As I said in a comment, you don't have a deadlock, but you starve your thread.
You have two options:
either you go for some threads with prioritized ordering
or you go for a concurrency enabled collection.
java.util.concurrent
has many examples of such collections. But beware - modifying a collection that is foreached is never a good idea. Maybe you just need a kind of a fifo queue? One threads adds a message and another pops it and evaluates?
For example there is ConcurrentLinkedQueue, that provides Add
method (to be used by your parseMessage
) and isEmpty()
(to be used in a while
loop of your other thread).
While I'm not a Java expert, I'll point you to this answer: How to use ConcurrentLinkedQueue? It provides some practical advices.