Multi threading with CachedThreadPool
up vote
0
down vote
favorite
I have server-client communication architecture where there is one server and 150 clients
The server-client communication happens via java NIO where all the clients send some or the other data every 10 seconds.
Previously we used to queue all the process messages and process all those in a single thread, as the number of clients are more so as the messages, server is not able to process all the messages instantly and there is a delay in processing in turn data loss.
So i have thought of implementing CachecThreadPool to process the tasks simultaneously as soon as they come, i have picked CachedThreadPool over FixedThreadPool because the tasks are short lived and many in number, below is the code for that.
The thread which receives messages from client calls ProcessorClass.processData(message)
as soon as it receives the message.
public class ProcessorClass{
private static final Logger LOGGER = Logger.getLogger(ProcessorClass.class);
static ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
public static void processData(StringBuffer message) {
Runnable task = new Runnable() {
@Override
public void run() {
try {
LOGGER.info("Queue size:"+executor.getQueue().size());
if (message != null){
processMessage(message);
}
}
catch(Exception e) {
LOGGER.error("Error happened in run() method" + e.getMessage());
}
}
};
executor.execute(task);
}
public static void processMessage(StringBuffer message){
// all the processing of message such as DB operations goes here.
}
}
Doubts:
- How CachedThreadPool stores the message in the queue because i haven't defined any explicitly.
- Should i chose FixedThreadPool over this?
- Should i make my
processMessage()
method synchronized?
All the suggestions and review comments are welcome.
java multithreading
New contributor
add a comment |
up vote
0
down vote
favorite
I have server-client communication architecture where there is one server and 150 clients
The server-client communication happens via java NIO where all the clients send some or the other data every 10 seconds.
Previously we used to queue all the process messages and process all those in a single thread, as the number of clients are more so as the messages, server is not able to process all the messages instantly and there is a delay in processing in turn data loss.
So i have thought of implementing CachecThreadPool to process the tasks simultaneously as soon as they come, i have picked CachedThreadPool over FixedThreadPool because the tasks are short lived and many in number, below is the code for that.
The thread which receives messages from client calls ProcessorClass.processData(message)
as soon as it receives the message.
public class ProcessorClass{
private static final Logger LOGGER = Logger.getLogger(ProcessorClass.class);
static ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
public static void processData(StringBuffer message) {
Runnable task = new Runnable() {
@Override
public void run() {
try {
LOGGER.info("Queue size:"+executor.getQueue().size());
if (message != null){
processMessage(message);
}
}
catch(Exception e) {
LOGGER.error("Error happened in run() method" + e.getMessage());
}
}
};
executor.execute(task);
}
public static void processMessage(StringBuffer message){
// all the processing of message such as DB operations goes here.
}
}
Doubts:
- How CachedThreadPool stores the message in the queue because i haven't defined any explicitly.
- Should i chose FixedThreadPool over this?
- Should i make my
processMessage()
method synchronized?
All the suggestions and review comments are welcome.
java multithreading
New contributor
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have server-client communication architecture where there is one server and 150 clients
The server-client communication happens via java NIO where all the clients send some or the other data every 10 seconds.
Previously we used to queue all the process messages and process all those in a single thread, as the number of clients are more so as the messages, server is not able to process all the messages instantly and there is a delay in processing in turn data loss.
So i have thought of implementing CachecThreadPool to process the tasks simultaneously as soon as they come, i have picked CachedThreadPool over FixedThreadPool because the tasks are short lived and many in number, below is the code for that.
The thread which receives messages from client calls ProcessorClass.processData(message)
as soon as it receives the message.
public class ProcessorClass{
private static final Logger LOGGER = Logger.getLogger(ProcessorClass.class);
static ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
public static void processData(StringBuffer message) {
Runnable task = new Runnable() {
@Override
public void run() {
try {
LOGGER.info("Queue size:"+executor.getQueue().size());
if (message != null){
processMessage(message);
}
}
catch(Exception e) {
LOGGER.error("Error happened in run() method" + e.getMessage());
}
}
};
executor.execute(task);
}
public static void processMessage(StringBuffer message){
// all the processing of message such as DB operations goes here.
}
}
Doubts:
- How CachedThreadPool stores the message in the queue because i haven't defined any explicitly.
- Should i chose FixedThreadPool over this?
- Should i make my
processMessage()
method synchronized?
All the suggestions and review comments are welcome.
java multithreading
New contributor
I have server-client communication architecture where there is one server and 150 clients
The server-client communication happens via java NIO where all the clients send some or the other data every 10 seconds.
Previously we used to queue all the process messages and process all those in a single thread, as the number of clients are more so as the messages, server is not able to process all the messages instantly and there is a delay in processing in turn data loss.
So i have thought of implementing CachecThreadPool to process the tasks simultaneously as soon as they come, i have picked CachedThreadPool over FixedThreadPool because the tasks are short lived and many in number, below is the code for that.
The thread which receives messages from client calls ProcessorClass.processData(message)
as soon as it receives the message.
public class ProcessorClass{
private static final Logger LOGGER = Logger.getLogger(ProcessorClass.class);
static ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
public static void processData(StringBuffer message) {
Runnable task = new Runnable() {
@Override
public void run() {
try {
LOGGER.info("Queue size:"+executor.getQueue().size());
if (message != null){
processMessage(message);
}
}
catch(Exception e) {
LOGGER.error("Error happened in run() method" + e.getMessage());
}
}
};
executor.execute(task);
}
public static void processMessage(StringBuffer message){
// all the processing of message such as DB operations goes here.
}
}
Doubts:
- How CachedThreadPool stores the message in the queue because i haven't defined any explicitly.
- Should i chose FixedThreadPool over this?
- Should i make my
processMessage()
method synchronized?
All the suggestions and review comments are welcome.
java multithreading
java multithreading
New contributor
New contributor
New contributor
asked 8 mins ago
raviraja
101
101
New contributor
New contributor
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
raviraja is a new contributor. Be nice, and check out our Code of Conduct.
raviraja is a new contributor. Be nice, and check out our Code of Conduct.
raviraja is a new contributor. Be nice, and check out our Code of Conduct.
raviraja 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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208210%2fmulti-threading-with-cachedthreadpool%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown