File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Urgent-synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Urgent-synchronization" Watch "Urgent-synchronization" New topic


Bahar Yilmaz

Joined: Feb 15, 2005
Posts: 1
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:

For PortListener's ProcessPacket method

Could u please help me urgentlyyy!!!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
I agree. Here's the link:
subject: Urgent-synchronization
jQuery in Action, 3rd edition