Hi I have an application with two threads running.One (SocketListener)is listennig to socket and getting packets and writing them to a hashtable.The other one (PacketHandler) is checking if a new packet has been written to the hashtable and if so, getting the packet and analysing the packet. In PacketHandler thread I have a static counter, which starts from zero and increment one when that packet is analysed.I have this structure in order to analyze each of them in orderly. But when multiple packet arrives at the same time the Packet handler sometimes takes the wrong packet from the hashtable to analyze (though the packets are put correctly) and skip the upper one and analyze the lower one twice indeed. The code is like for PacketHandler:
You might see if you can replace the map with a collection that retains the insertion order. In JDK 5 you get the Queue interface and a slick BlockingQueue implementation. In earlier JDKs you could build FIFO processing around LinkedList.
Are you leaving all packets in the map forever? That could grow to consume all your memory in time!
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi