3
votes

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??

1
You have kept ExecutorService service as final and also you are assigning new values to service service=Executors.newFixedThreadPool(5); in while loop. It is not clear what you are doing? - Naman Gala
@NamanGala-Sorry, it was a mistake and I wanted to add that line inside comment, left it. Anyways, I've edited the question and removed final from the ExecutorService... Please answer now. - asad
You have to take out your initialization of ExecutorService from while loop. - Naman Gala
@NamanGala-Did that, what else? Thanks for helping. I request you to write an answer if you're quite confident enough... - asad
I am just helping you to debug your code. See you have now initialized ExecutorService of fixed 5 thread. Now do one thing instead of using while for infinite loop, you can use for loop to execute it for 10 times and see how many threads are alive at a time. And try to add thread number in MultithreadedInvocation constructor 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

1 Answers

2
votes

If you have 5 threads then decide to run a loop that could call up to 30 threads, those processes are put into a queue and wait for a thread to become available.

Your 6th thread will wait until a previously submitted thread finishes or is cancelled.

Previous post.