Receiving DatagramSocket without Blocking?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   silvia_antunes
Posted On:   Sunday, July 28, 2002 08:34 AM

Hello everyone.. I have used both service method and doGet/doPost methods while writing servlets, I do really dont understand the difference in between overriding the service method with doGet/doPost methods or using service method instead. Please give me a clear and detail information on it. Thanks in advance Deepya. I have one thread to receive a DatagramPacket and other threads running at the same time, so I can't block in the instruction DatagramSocket.receive(DatagramPacket) because the other threads won't run. Is it possible to receive a DatagramSocket without Blocking a Thread? public class threadreceive extends Thread { int count = 5; /**   More>>



Hello everyone..
I have used both service method and doGet/doPost methods while writing servlets, I do really dont understand the difference in between overriding the service method with doGet/doPost methods or using service method instead.
Please give me a clear and detail information on it.


Thanks in advance
Deepya.

I have one thread to receive a DatagramPacket and other threads running at the same time, so I can't block in the instruction DatagramSocket.receive(DatagramPacket) because the other threads won't run.
Is it possible to receive a DatagramSocket without Blocking a Thread?



public class threadreceive extends Thread {

int count = 5;



/** Creates new threadreceive */

public threadreceive() {

}



public void start() {

byte[] receiveData = new byte[1024];

byte[] sendData = new byte[1024];


try{

DatagramSocket serverSocket = new DatagramSocket();

DatagramPacket receivePacket = new DatagramPacket (receiveData, receiveData.length);



while (count-- >0){

try{

serverSocket.receive(receivePacket);

Thread.sleep(10);



} catch (InterruptedException e){

} catch (java.io.InterruptedIOException e){

}

}



} catch(SocketException e) {

} catch (IOException e) {



}

}

   <<Less

Re: Receiving DatagramSocket without Blocking?

Posted By:   Mike_Friedrich  
Posted On:   Sunday, July 28, 2002 11:37 PM

SUN's API doc says:

"This method blocks until a datagram is received."


This means, that the current thread will block, not the others. If you see that other threads block while this methods runs - you have a synchronized anythere around them.


Try to avaoid synchronization around code that could run a long time (>0,5 seconds) or for code where you dont know the time to run.


Ups, i see now you have overridden the start() method, this is definitly wrong. Use run() for that.

In your case a new thread is not created. The overridden method creates a new one and launches run in that new thread.


Regards, Mike

About | Sitemap | Contact