Cannot create more thread to process socket(Block IO) by a threadpool











up vote
0
down vote

favorite












I want to create a new thead and assign it to a thread pool executor so that it can be processed concurrently. The core size of such a thread pool is 5 and maximum of it is 20. However it cannot create more threads than core size in the code below. If I uncommented the code if (socket != null) { socket.close(); } (Actually, I can't close it here as the socket should be processed in the TestHandler), more threads can be created upto maximum of the thread pool which is 20.
So why this can happened? Does anyone can help to explain it?
The workQueue size can be adjusted accordingly and I was using apache ab to do test. e.g. ab -n 1000 -c 10 http://localhost:8080/



import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import java.util.concurrent.*;

public class MultiThreadServer {
public static void main(String args) throws IOException {
MultiThreadServer server = new MultiThreadServer();
server.start();
}

private static ThreadFactory builder = new ThreadFactoryBuilder().setNameFormat("Demo Task Executor #%d").build();

public void start() throws IOException {
ServerSocket serverSocket = new ServerSocket(8080);
Socket socket = null;
ExecutorService executor = new ThreadPoolExecutor(5,
20,
10,
TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(10),
builder);
for (; ; ) {
socket = serverSocket.accept();

executor.execute(new TestHandler(socket));
// if (socket != null) {
// socket.close();
// }
}
}

class TestHandler implements Runnable {
private Socket socket;

public TestHandler(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
try {
Thread.sleep(new Random().nextInt(100));
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread());
}
}

}









share|improve this question







New contributor




Qiannan L. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    I want to create a new thead and assign it to a thread pool executor so that it can be processed concurrently. The core size of such a thread pool is 5 and maximum of it is 20. However it cannot create more threads than core size in the code below. If I uncommented the code if (socket != null) { socket.close(); } (Actually, I can't close it here as the socket should be processed in the TestHandler), more threads can be created upto maximum of the thread pool which is 20.
    So why this can happened? Does anyone can help to explain it?
    The workQueue size can be adjusted accordingly and I was using apache ab to do test. e.g. ab -n 1000 -c 10 http://localhost:8080/



    import com.google.common.util.concurrent.ThreadFactoryBuilder;

    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Random;
    import java.util.concurrent.*;

    public class MultiThreadServer {
    public static void main(String args) throws IOException {
    MultiThreadServer server = new MultiThreadServer();
    server.start();
    }

    private static ThreadFactory builder = new ThreadFactoryBuilder().setNameFormat("Demo Task Executor #%d").build();

    public void start() throws IOException {
    ServerSocket serverSocket = new ServerSocket(8080);
    Socket socket = null;
    ExecutorService executor = new ThreadPoolExecutor(5,
    20,
    10,
    TimeUnit.MILLISECONDS,
    new ArrayBlockingQueue<>(10),
    builder);
    for (; ; ) {
    socket = serverSocket.accept();

    executor.execute(new TestHandler(socket));
    // if (socket != null) {
    // socket.close();
    // }
    }
    }

    class TestHandler implements Runnable {
    private Socket socket;

    public TestHandler(Socket socket) {
    this.socket = socket;
    }

    @Override
    public void run() {
    try {
    Thread.sleep(new Random().nextInt(100));
    socket.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println(Thread.currentThread());
    }
    }

    }









    share|improve this question







    New contributor




    Qiannan L. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to create a new thead and assign it to a thread pool executor so that it can be processed concurrently. The core size of such a thread pool is 5 and maximum of it is 20. However it cannot create more threads than core size in the code below. If I uncommented the code if (socket != null) { socket.close(); } (Actually, I can't close it here as the socket should be processed in the TestHandler), more threads can be created upto maximum of the thread pool which is 20.
      So why this can happened? Does anyone can help to explain it?
      The workQueue size can be adjusted accordingly and I was using apache ab to do test. e.g. ab -n 1000 -c 10 http://localhost:8080/



      import com.google.common.util.concurrent.ThreadFactoryBuilder;

      import java.io.IOException;
      import java.net.ServerSocket;
      import java.net.Socket;
      import java.util.Random;
      import java.util.concurrent.*;

      public class MultiThreadServer {
      public static void main(String args) throws IOException {
      MultiThreadServer server = new MultiThreadServer();
      server.start();
      }

      private static ThreadFactory builder = new ThreadFactoryBuilder().setNameFormat("Demo Task Executor #%d").build();

      public void start() throws IOException {
      ServerSocket serverSocket = new ServerSocket(8080);
      Socket socket = null;
      ExecutorService executor = new ThreadPoolExecutor(5,
      20,
      10,
      TimeUnit.MILLISECONDS,
      new ArrayBlockingQueue<>(10),
      builder);
      for (; ; ) {
      socket = serverSocket.accept();

      executor.execute(new TestHandler(socket));
      // if (socket != null) {
      // socket.close();
      // }
      }
      }

      class TestHandler implements Runnable {
      private Socket socket;

      public TestHandler(Socket socket) {
      this.socket = socket;
      }

      @Override
      public void run() {
      try {
      Thread.sleep(new Random().nextInt(100));
      socket.close();
      } catch (Exception e) {
      e.printStackTrace();
      }
      System.out.println(Thread.currentThread());
      }
      }

      }









      share|improve this question







      New contributor




      Qiannan L. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I want to create a new thead and assign it to a thread pool executor so that it can be processed concurrently. The core size of such a thread pool is 5 and maximum of it is 20. However it cannot create more threads than core size in the code below. If I uncommented the code if (socket != null) { socket.close(); } (Actually, I can't close it here as the socket should be processed in the TestHandler), more threads can be created upto maximum of the thread pool which is 20.
      So why this can happened? Does anyone can help to explain it?
      The workQueue size can be adjusted accordingly and I was using apache ab to do test. e.g. ab -n 1000 -c 10 http://localhost:8080/



      import com.google.common.util.concurrent.ThreadFactoryBuilder;

      import java.io.IOException;
      import java.net.ServerSocket;
      import java.net.Socket;
      import java.util.Random;
      import java.util.concurrent.*;

      public class MultiThreadServer {
      public static void main(String args) throws IOException {
      MultiThreadServer server = new MultiThreadServer();
      server.start();
      }

      private static ThreadFactory builder = new ThreadFactoryBuilder().setNameFormat("Demo Task Executor #%d").build();

      public void start() throws IOException {
      ServerSocket serverSocket = new ServerSocket(8080);
      Socket socket = null;
      ExecutorService executor = new ThreadPoolExecutor(5,
      20,
      10,
      TimeUnit.MILLISECONDS,
      new ArrayBlockingQueue<>(10),
      builder);
      for (; ; ) {
      socket = serverSocket.accept();

      executor.execute(new TestHandler(socket));
      // if (socket != null) {
      // socket.close();
      // }
      }
      }

      class TestHandler implements Runnable {
      private Socket socket;

      public TestHandler(Socket socket) {
      this.socket = socket;
      }

      @Override
      public void run() {
      try {
      Thread.sleep(new Random().nextInt(100));
      socket.close();
      } catch (Exception e) {
      e.printStackTrace();
      }
      System.out.println(Thread.currentThread());
      }
      }

      }






      java multithreading threadpool serversocket






      share|improve this question







      New contributor




      Qiannan L. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Qiannan L. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Qiannan L. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 5 at 3:48









      Qiannan L.

      11




      11




      New contributor




      Qiannan L. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Qiannan L. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Qiannan L. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          If you check the implementation of execute method in ExecutorService class you can see the following code:



          //...................

          if (isRunning(c) && workQueue.offer(command)) {
          //...................
          }
          else if (!addWorker(command, false))
          reject(command);

          //...................


          So a new thread will be created only if the workQueue is full. You have initialized your pool with new ArrayBlockingQueue<>(10). That means no new threads will be created for the first 15 concurrent requests (corePoolSize + queue capacity).



          To see for example 6 threads just try to run ab -n 1000 -c 16 http://localhost:8080/ or reduce the queue initial capacity. But take into account that when the queue is full and the number of thread equals maximumPullSize any new request will be rejected.






          share|improve this answer








          New contributor




          Alex Khalin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • I know that. It still doesn't work if I change the ArrayBlockingQueue<>(1) size to 1 or increase the ab concurent threads. The maximun num of created threads is still 5 unless I close the socket explicitly. My jvm is java version "1.8.0_181" Java(TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
            – Qiannan L.
            Nov 6 at 21:23













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Qiannan L. is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148058%2fcannot-create-more-thread-to-process-socketblock-io-by-a-threadpool%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          If you check the implementation of execute method in ExecutorService class you can see the following code:



          //...................

          if (isRunning(c) && workQueue.offer(command)) {
          //...................
          }
          else if (!addWorker(command, false))
          reject(command);

          //...................


          So a new thread will be created only if the workQueue is full. You have initialized your pool with new ArrayBlockingQueue<>(10). That means no new threads will be created for the first 15 concurrent requests (corePoolSize + queue capacity).



          To see for example 6 threads just try to run ab -n 1000 -c 16 http://localhost:8080/ or reduce the queue initial capacity. But take into account that when the queue is full and the number of thread equals maximumPullSize any new request will be rejected.






          share|improve this answer








          New contributor




          Alex Khalin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • I know that. It still doesn't work if I change the ArrayBlockingQueue<>(1) size to 1 or increase the ab concurent threads. The maximun num of created threads is still 5 unless I close the socket explicitly. My jvm is java version "1.8.0_181" Java(TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
            – Qiannan L.
            Nov 6 at 21:23

















          up vote
          0
          down vote













          If you check the implementation of execute method in ExecutorService class you can see the following code:



          //...................

          if (isRunning(c) && workQueue.offer(command)) {
          //...................
          }
          else if (!addWorker(command, false))
          reject(command);

          //...................


          So a new thread will be created only if the workQueue is full. You have initialized your pool with new ArrayBlockingQueue<>(10). That means no new threads will be created for the first 15 concurrent requests (corePoolSize + queue capacity).



          To see for example 6 threads just try to run ab -n 1000 -c 16 http://localhost:8080/ or reduce the queue initial capacity. But take into account that when the queue is full and the number of thread equals maximumPullSize any new request will be rejected.






          share|improve this answer








          New contributor




          Alex Khalin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • I know that. It still doesn't work if I change the ArrayBlockingQueue<>(1) size to 1 or increase the ab concurent threads. The maximun num of created threads is still 5 unless I close the socket explicitly. My jvm is java version "1.8.0_181" Java(TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
            – Qiannan L.
            Nov 6 at 21:23















          up vote
          0
          down vote










          up vote
          0
          down vote









          If you check the implementation of execute method in ExecutorService class you can see the following code:



          //...................

          if (isRunning(c) && workQueue.offer(command)) {
          //...................
          }
          else if (!addWorker(command, false))
          reject(command);

          //...................


          So a new thread will be created only if the workQueue is full. You have initialized your pool with new ArrayBlockingQueue<>(10). That means no new threads will be created for the first 15 concurrent requests (corePoolSize + queue capacity).



          To see for example 6 threads just try to run ab -n 1000 -c 16 http://localhost:8080/ or reduce the queue initial capacity. But take into account that when the queue is full and the number of thread equals maximumPullSize any new request will be rejected.






          share|improve this answer








          New contributor




          Alex Khalin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          If you check the implementation of execute method in ExecutorService class you can see the following code:



          //...................

          if (isRunning(c) && workQueue.offer(command)) {
          //...................
          }
          else if (!addWorker(command, false))
          reject(command);

          //...................


          So a new thread will be created only if the workQueue is full. You have initialized your pool with new ArrayBlockingQueue<>(10). That means no new threads will be created for the first 15 concurrent requests (corePoolSize + queue capacity).



          To see for example 6 threads just try to run ab -n 1000 -c 16 http://localhost:8080/ or reduce the queue initial capacity. But take into account that when the queue is full and the number of thread equals maximumPullSize any new request will be rejected.







          share|improve this answer








          New contributor




          Alex Khalin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          Alex Khalin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered Nov 5 at 20:28









          Alex Khalin

          11




          11




          New contributor




          Alex Khalin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Alex Khalin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Alex Khalin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.












          • I know that. It still doesn't work if I change the ArrayBlockingQueue<>(1) size to 1 or increase the ab concurent threads. The maximun num of created threads is still 5 unless I close the socket explicitly. My jvm is java version "1.8.0_181" Java(TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
            – Qiannan L.
            Nov 6 at 21:23




















          • I know that. It still doesn't work if I change the ArrayBlockingQueue<>(1) size to 1 or increase the ab concurent threads. The maximun num of created threads is still 5 unless I close the socket explicitly. My jvm is java version "1.8.0_181" Java(TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
            – Qiannan L.
            Nov 6 at 21:23


















          I know that. It still doesn't work if I change the ArrayBlockingQueue<>(1) size to 1 or increase the ab concurent threads. The maximun num of created threads is still 5 unless I close the socket explicitly. My jvm is java version "1.8.0_181" Java(TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
          – Qiannan L.
          Nov 6 at 21:23






          I know that. It still doesn't work if I change the ArrayBlockingQueue<>(1) size to 1 or increase the ab concurent threads. The maximun num of created threads is still 5 unless I close the socket explicitly. My jvm is java version "1.8.0_181" Java(TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
          – Qiannan L.
          Nov 6 at 21:23












          Qiannan L. is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Qiannan L. is a new contributor. Be nice, and check out our Code of Conduct.













          Qiannan L. is a new contributor. Be nice, and check out our Code of Conduct.












          Qiannan L. is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148058%2fcannot-create-more-thread-to-process-socketblock-io-by-a-threadpool%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Hercules Kyvelos

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud