I am facing this problem where one thread keeps adding packets which it receives from the network to linkedlist. And the other thread retrieves it from the linked list.
The below is the code of the Thread which keeps receiving the packets and add's them to list.

private class ReceivingPacket implements Runnable
    {

        public void run()
        {
            packet =new DatagramPacket(new byte[8210],8210);
            try {
                while(true)
                {
                   socket.receive(packet);
                   list.add(packet);
                   
                   
                 }

The below code gets packet from the list and processes them. packet2 is a DatagramPacket

if(list.size()>0)
        {
            packet2=list.getFirst();//.................................1
            System.out.println(packet2);
            buf = packet2.getData();...................................2
            System.out.println(list.size()+ " ");
            jpacket = new Jpacket(buf);
            list.remove();
            break;

        }
        else
        {....

Now i have observed that [1] works fine. But after [2] the code doesnt move forward.
even if i print packet2.getLength(). The code doesnt run and the Thread doesnt proceed.
Pl help

Does [2] run in its own thread?
Presumably it needs to wait until there something in the list?
If so, the normal solution is to use a LinkedBlockingQueue - you will find the documentation in the usual places.

Does [2] run in its own thread?
Presumably it needs to wait until there something in the list?
If so, the normal solution is to use a LinkedBlockingQueue - you will find the documentation in the usual places.

If i add a print method before that statement, it prints. But once it reaches packet2.getData() line, it doesnt move forward

Yes the statement [2] is in a different Thread,actually the main thread

Have you confirmed that packet2 is not null?
Is it possible you have a thread deadlock?
Anyway, unless list is a BlockingQueue I don't see how this is going to work.

This article has been dead for over six months. Start a new discussion instead.