问题
I have 2 java threads receiving UDP multicast.
Each thread will receive UDP packets from different multicast streams of say around 60 Mbps, then processes UDP packets and queues in shared BlockingQueue
. UDP packets are of around 1300 bytes.
I want order of processed UDP packets in queue, which is same as how packets reached to NIC of machine, even if receiving packets from different threads(different multicast streams).
I am not able to achieve this in these threads, that is my problem.
Is it possible using java, or should I use some other language like C?
EDIT
Basically, threads will receive multicast from different multicast streams, and packet order in queue will depend on order how receiving threads executed to take packets from NIC, but I want order to be same as how packets reached to NIC.
I tried lowering UDP receive buffer for each stream, packets were getting in queue almost in order but packets started losing. If I increase receive buffer, order in queue completely depends on how receiving threads executed.
Please help me on this. Is it possible?
回答1:
Sequence numbers? Define a stream-control class that contains a sequence-number that is incremented and loaded into each buffer object received from that stream, a 'last-processed' number that the pool threads can check and a list for every out-of order buffer that is dequeued by your pool.
The pool threads check to see if the buffer they dequeue has the correct sequence-number, (last-processed+1). If so, they process that buffer and any subsequent numbers in the listed buffers. If not, they add that buffer to the list for later processing.
PS: The stream-control class methods will need locking up, for sure.
来源:https://stackoverflow.com/questions/20274735/java-multiple-threads-taking-turn