Iam stuck in this problem and have spent almost 5-6 hours in it.
In the project i am implementing TCP over UDP.
When stream.activate() is called the following new Thread is implemented
Now the main Thread checks if list has any packets in it
It implements the following code. Basically it checks if there list has any packets in it, if it has it get's the packet and then processes. But some how it doesnt proceed
Now the problem is as soon as the process reaches , it doesnt proceed forward. It cant be deadlock as their is nothing for deadlock.
I don't think you gave us enough code to know what is going wrong. A functional SSCCE is probably required.
Anything that is sent across multiple threads needs to be protected. In the Thread with 'socket.receive(packet);', how is the packet being made, or stored? How about the buffer that becomes part of the packet? Any re-use of the packet or the buffer will cause bad things. You should re-create both the packet and the buffer for each packet the socket tries to receive.
What is actually happening when the main thread 'doesn't proceed any further'? Have you run a profiler to see what the wait is? Are you sure there isn't some exception occuring that you aren't handling appropriately?
I don't know a lot about DatagramPackets, but maybe you should do the getData() call from inside the Thread which receives the packet, and put the results (the byte) into the List to be processed later.
Joined: Sep 04, 2009
Thanks a lot of help.
The socket is an reference for DatagramSocket, and socket.receive(packet) is an inbuilt method which copies information from the packet which it receives from the network and puts the data in packet along with other information like destination address etc.
The reason why i dont execute retrieving the data in the the other thread is, because as you know in UDP there is no internal buffer, hence as soon as the packet arrives from the network it should be received or there will be a packet loss. Hence if i include the code which retrieves data from list and processes it, a lot of processor power will go into that (as in creation of jpacket there are a lot of algorithms implemented in it according to the data in packet).
So in the mean time iam loosing a lot of data. I actually tried that. That way i was almost loosing 50% of the packets.
PS: I have attached the code . Please help if you can
In the file. First say 'in' is an instance of SocketInputStream().
in.activate() is called which initiates a new Thread which only collects packets from network and add's it to the list.
Later when in.readData() is called : It removes packets from the list and later does calculation on that.
That isn't an SSCCE, I can't really run it to see what is happening (without a lot of work on my end). Can you make a small example that I can copy, compile, and see what is going on?
From what I can tell, though: This has little to do with the collection. It may have something to do with the byte, the DatagramPacket, or something else. But if the code gets to the buf = packet2.getData(); line, after the System.out statement, then you know that the collection has done its job. What is left is why getData() doesn't do its job, and to explain why and how you think that statement 'is not moving forward'.
Explain more about how you know it is the getData() call that is causing the problem, and how you know that getData() doesn't move forward, versus perhaps a mishandled exception occurring.
Joined: Sep 04, 2009
Hi Steve, First of all Happy Diwali (Diwali is the biggest festival here in India)
I think i have found the solution to the problem. THe data is completely received. The thing it that can you please explain System.out.println(). Because the problem is if i make 2 threads and run them i
This code only receives the data,
to send packets you can use the below code
Now Run the 1st code and wait for around 20-30 sec to execute the 2nd code.why is it that  is not printed when 1st code is implemented and only after 2nd code runs the output is shown.
Similarly in my problem i have found that all the data is arriving but it is not printing on screen.
subject: Threading and collections problem.Pl help