As per this explanation given in Javadocs, it says the following about
public static ExecutorService newFixedThreadPool(int nThreads)
Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.
Which queue are they talking about? What if I don't use any queue in my multi-threaded application, like the below :
ExecutorService service;
service=Executors.newFixedThreadPool(5);
while(true){
try {
s=ss.accept();
//new Thread(new MultithreadedInvocation(s)).start();
service.submit(new MultithreadedInvocation(s)).get();
} catch (InterruptedException | ExecutionException ex) {
ex.printStackTrace();
}
MultithreadedInvocation.java
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class MultithreadedInvocation implements Runnable{
//initialize in const'r
private final Socket socket;
public MultithreadedInvocation(Socket s) {
this.socket=s;
}
@Override
public void run() {
try {
DataInputStream din=new DataInputStream(socket.getInputStream());
DataOutputStream dout=new DataOutputStream(socket.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int str;
str=din.read();
String name=din.readUTF();
System.out.println("Client Name = "+name);
System.out.println("Actual Client requested for file index "+str+".");
ClientInfo ci = new ClientInfo();
ci.ClientName=name;
ci.ClientFileChoice=str;
String fileName = new FileMapping().lookupFile(str);
File tempFile=new File("C:\\Users\\server-3\\Desktop\\List\\"+fileName);
dout.writeLong(tempFile.length());
dout.flush();
din.close();
dout.close();
socket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
What will happen to my 6th thread in this case, will it be automatically added to that unknown queue, or the thread-pool will terminate, and it won't function further??
ExecutorService serviceas final and also you are assigning new values to serviceservice=Executors.newFixedThreadPool(5);in while loop. It is not clear what you are doing? - Naman Galafinalfrom the ExecutorService... Please answer now. - asadMultithreadedInvocationconstructor along with socket and assign different thread numbers into it. In your run method you can sysout entry and exit along with thead number. - Naman Gala