Hi am building a Peer2Peer network using UDP socket each time the server receives a message from the client it create a thread to deal with the request.am trying to implement a ThreadPool Queue to store the request and limit the number of thread running on the machine and whenever a thread is free we get threads from the queue (Producer/Consumer),i have been trying for a while now but the new threads don't get added on the queue i end executing only the first thread that was added.
public void run() {
BlockingQueue<Runnable> queue=new LinkedBlockingQueue<Runnable>(10);
ExecutorService tpes =Executors.newFixedThreadPool(10);
int server_port = 8767;
DatagramSocket s = null;
while(true){
byte[] message = new byte[1024];
DatagramPacket p = new DatagramPacket(message, message.length);
try{
s = new DatagramSocket(server_port);
}catch (SocketException e) {
e.printStackTrace();
System.out.println("Socket excep");
}
try {
s.receive(p);
queue.put(new RequestHandler(p));
tpes.execute(queue.take());
}catch (Exception e) {
e.printStackTrace();
System.out.println("IO EXcept");
}
finally{
s.close();
}
}
}
I don't know where am going wrong ? any help would be appreciated
RequestHandler
? You create strange recursion. Also if you.take()
the object you've just put there a few lines earlier you could just use it directly. – zapl