0

The problem is that when we use multiple threads and each containing System.out.println(). It doesnt print at time according to what i think it shoud
On implementing the below 2 codes:

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;


class E

{
    LinkedBlockingQueue<DatagramPacket> li = new LinkedBlockingQueue<DatagramPacket>();
    DatagramPacket packet = new DatagramPacket(new byte[8210],8210);
    DatagramPacket packet2;
    public static void main(String args[])
    {
        new E().go2();
    }
    void go2()
    {
    new Thread(new Foo()).start();
    new Thread(new Fo()).start();

}
int j,k;
class Fo implements Runnable
{

        public void run()
        {

            DatagramSocket so = null;
            try
            {
                so = new DatagramSocket(9001);
            }
            catch (SocketException ex)
            {
                System.err.println("Error");
            }
            while(true){
            try {
                k++;
                so.receive(packet);
                synchronized(li)
                {
                    li.add(packet);}
                    if(k==20)// ie if i receive 20 packets then break
                    {
                        break;
                    }
                    
                }
                catch(Exception ex)
            {
                   System.err.println("error");
                }

            }
        }


}

class Foo implements Runnable
        {

        public void run() {
            int i=0;
            System.out.print("check  ");
            while(true)

            {
                try
                {
                    
                    if(li.size()>0)
                    {
                        i++;
                         synchronized(li){
                        packet2 = li.remove();}
                        j= k-i;
                        System.out.println(j+" "+new String(packet2.getData()));
                        if(i==20)
                        {
                            break;
                        }
                        
                    }
                  else
                    {
                    Thread.sleep(100);
                    System.out.print("slept"+  "   ");.....................................................................................................[1]
                     }}
                    catch (InterruptedException ex) {
                    Logger.getLogger(E.class.getName()).log(Level.SEVERE, null, ex);
                }


            }System.out.println("\n"+j);
        }

}
}

This code only receives the data,
to send packets you can use the below code

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Random;

public class prac7 {
     public static void main (String[] args) throws SocketException, UnknownHostException, IOException, InterruptedException
     {
        new prac7().go();
     }
     void go() throws SocketException, UnknownHostException, IOException, InterruptedException
    {
         DatagramSocket s= new DatagramSocket();
         String as = " hey what is the matter";
         DatagramPacket p = new DatagramPacket(as.getBytes(),as.length(),InetAddress.getByName("127.0.0.1"),9001);
         while(true)
         {
         s.send(p);
         Thread.sleep(100);
        }

     }
}

Now Run the 1st code and wait for around 20-30 sec to execute the 2nd code.why is it that [1] is not printed when 1st code is implemented and only after 2nd code runs the output is shown.

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by purijatin
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.