Instead of
pendingmsgs.remove(pendingmsg);
use
itpendingmsgs.remove();
See:
The behavior of an iterator is unspecified if the underlying
collection is modified while the iteration is in progress in any way
other than by calling this method.
Source: Java API