Venkat Papana
Venkat Papana

Reputation: 4927

Java Concurrency: Can I create a pool using worker thread

I'm new to java.uti.concurrent package. I'm trying to develop a socket listner class by using ExecutorService. Here is my Main code snippet:

    while (!getExit()) {

        try{
            logger.info("RequestListner.run(): listening for new request...");
            Socket socket = server.accept();
            logger.info("RequestListner.run(): got new request");
            MyTask task = new MyTask(socket);
            pool.submit(task);
            logger.info("RequestListner.run(): submitted new request to pool");
        }catch(Exception e) {
            logger.error("RequestListner.run(): Exception: "+e.getMessage());
        }
    }

My MyTask class have a ExecutorService object, it will submit MyTask2 tasks. (my intension is to exeute multi thread processing for my each socket connection)

My application is accepting my socket client request and creating pool of MyTask2, executing fine. But is accepting my second socket client connection only after finishing my frist request.

Can anybody please let me know how to fix this.

Thanks in advance.

-Venkat Papana

Upvotes: 0

Views: 193

Answers (2)

Ammar
Ammar

Reputation: 2437

What kind of Executor or ExecutorService have you created ?

public static final POOL_SIZE = 5;
ExecutorService exec = Executors.newFixedThreadPool(POOL_SIZE);

Upvotes: 1

Suraj Chandran
Suraj Chandran

Reputation: 24791

You need not create a "pool" of ExecutorService, just use one ExecutorService and it itself manages a pool of threads internally.

And also your implementation of ExecutorService should be able to execute given number of tasks paralelly. For e.g., use Executors.new*ThreadPool() methods in it and parameterize it accordingly.

Upvotes: 0

Related Questions