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());
}
}
}
java multithreading threadpool serversocket
New contributor
add a comment |
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());
}
}
}
java multithreading threadpool serversocket
New contributor
add a comment |
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());
}
}
}
java multithreading threadpool serversocket
New contributor
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
java multithreading threadpool serversocket
New contributor
New contributor
New contributor
asked Nov 5 at 3:48
Qiannan L.
11
11
New contributor
New contributor
add a comment |
add a comment |
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.
New contributor
I know that. It still doesn't work if I change theArrayBlockingQueue<>(1)
size to 1 or increase theab
concurent threads. The maximun num of created threads is still 5 unless I close the socket explicitly. My jvm isjava 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
add a comment |
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.
New contributor
I know that. It still doesn't work if I change theArrayBlockingQueue<>(1)
size to 1 or increase theab
concurent threads. The maximun num of created threads is still 5 unless I close the socket explicitly. My jvm isjava 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
add a comment |
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.
New contributor
I know that. It still doesn't work if I change theArrayBlockingQueue<>(1)
size to 1 or increase theab
concurent threads. The maximun num of created threads is still 5 unless I close the socket explicitly. My jvm isjava 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
add a comment |
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.
New contributor
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.
New contributor
New contributor
answered Nov 5 at 20:28
Alex Khalin
11
11
New contributor
New contributor
I know that. It still doesn't work if I change theArrayBlockingQueue<>(1)
size to 1 or increase theab
concurent threads. The maximun num of created threads is still 5 unless I close the socket explicitly. My jvm isjava 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
add a comment |
I know that. It still doesn't work if I change theArrayBlockingQueue<>(1)
size to 1 or increase theab
concurent threads. The maximun num of created threads is still 5 unless I close the socket explicitly. My jvm isjava 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
add a comment |
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.
Qiannan L. is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password