Unpredictable behavior in Java Thread Pool while capturing response for multiple threads
up vote
0
down vote
favorite
I am trying to add 10 records to different server with the help of thread pool and waiting for response to get the information added on server side, but when I execute the code at my end I am getting same response for few threads, but data stored at server end are unique values per record.
ExecutorService threadPool = Executors.newFixedThreadPool(10);
List<Future<Employee>> listOfEmployee = new ArrayList<Future<Employee>>();
List<Employee> employeeList = new ArrayList<Employee>();
for(int i=0;i<10;i++)
{
listOfEmployee.add(threadPool.submit(new Callable<Employee>() {
@Override
public Employee call()
{
return employee.add();
}
}));
}
threadPool.shutdown();
threadPool.awaitTermination(100,TimeUnit.MILLISECONDS);
for(Future<Employee> future : listOfEmployee)
{
employeeList.add(future.get());
}
what can be the root cause for the problem?
java multithreading threadpool
|
show 2 more comments
up vote
0
down vote
favorite
I am trying to add 10 records to different server with the help of thread pool and waiting for response to get the information added on server side, but when I execute the code at my end I am getting same response for few threads, but data stored at server end are unique values per record.
ExecutorService threadPool = Executors.newFixedThreadPool(10);
List<Future<Employee>> listOfEmployee = new ArrayList<Future<Employee>>();
List<Employee> employeeList = new ArrayList<Employee>();
for(int i=0;i<10;i++)
{
listOfEmployee.add(threadPool.submit(new Callable<Employee>() {
@Override
public Employee call()
{
return employee.add();
}
}));
}
threadPool.shutdown();
threadPool.awaitTermination(100,TimeUnit.MILLISECONDS);
for(Future<Employee> future : listOfEmployee)
{
employeeList.add(future.get());
}
what can be the root cause for the problem?
java multithreading threadpool
It looks like you can completely remove the threads from this problem and does it work then? What does employee.add do? What is employee?
– matt
Nov 22 at 7:33
yes when we remove the threads it works completely fine, and our employee.add() auto generated all the attributes of employee and add to the list present in different server and send a response from server of Employee type.
– Adarsh Patel
Nov 22 at 8:59
You are using thread-unsafe data structures and operations.
– user207421
Nov 22 at 9:39
You'll need to include the employee.add method to get any help then, also you need to state some explicit quantity that is "incorrect" eg. On the server it is "A" bet the values in employeeList all contain "B".
– matt
Nov 22 at 9:44
Output we received : CompanyId : 0 EmployeeId : 9 CompanyId : 0 EmployeeId : 8 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 1 CompanyId : 0 EmployeeId : 9 CompanyId : 0 EmployeeId : 3 CompanyId : 0 EmployeeId : 0 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 6 Here we can see that there is two Id’s 6 and 9 are repeated.
– Adarsh Patel
Nov 22 at 11:02
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to add 10 records to different server with the help of thread pool and waiting for response to get the information added on server side, but when I execute the code at my end I am getting same response for few threads, but data stored at server end are unique values per record.
ExecutorService threadPool = Executors.newFixedThreadPool(10);
List<Future<Employee>> listOfEmployee = new ArrayList<Future<Employee>>();
List<Employee> employeeList = new ArrayList<Employee>();
for(int i=0;i<10;i++)
{
listOfEmployee.add(threadPool.submit(new Callable<Employee>() {
@Override
public Employee call()
{
return employee.add();
}
}));
}
threadPool.shutdown();
threadPool.awaitTermination(100,TimeUnit.MILLISECONDS);
for(Future<Employee> future : listOfEmployee)
{
employeeList.add(future.get());
}
what can be the root cause for the problem?
java multithreading threadpool
I am trying to add 10 records to different server with the help of thread pool and waiting for response to get the information added on server side, but when I execute the code at my end I am getting same response for few threads, but data stored at server end are unique values per record.
ExecutorService threadPool = Executors.newFixedThreadPool(10);
List<Future<Employee>> listOfEmployee = new ArrayList<Future<Employee>>();
List<Employee> employeeList = new ArrayList<Employee>();
for(int i=0;i<10;i++)
{
listOfEmployee.add(threadPool.submit(new Callable<Employee>() {
@Override
public Employee call()
{
return employee.add();
}
}));
}
threadPool.shutdown();
threadPool.awaitTermination(100,TimeUnit.MILLISECONDS);
for(Future<Employee> future : listOfEmployee)
{
employeeList.add(future.get());
}
what can be the root cause for the problem?
java multithreading threadpool
java multithreading threadpool
asked Nov 22 at 7:23
Adarsh Patel
42
42
It looks like you can completely remove the threads from this problem and does it work then? What does employee.add do? What is employee?
– matt
Nov 22 at 7:33
yes when we remove the threads it works completely fine, and our employee.add() auto generated all the attributes of employee and add to the list present in different server and send a response from server of Employee type.
– Adarsh Patel
Nov 22 at 8:59
You are using thread-unsafe data structures and operations.
– user207421
Nov 22 at 9:39
You'll need to include the employee.add method to get any help then, also you need to state some explicit quantity that is "incorrect" eg. On the server it is "A" bet the values in employeeList all contain "B".
– matt
Nov 22 at 9:44
Output we received : CompanyId : 0 EmployeeId : 9 CompanyId : 0 EmployeeId : 8 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 1 CompanyId : 0 EmployeeId : 9 CompanyId : 0 EmployeeId : 3 CompanyId : 0 EmployeeId : 0 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 6 Here we can see that there is two Id’s 6 and 9 are repeated.
– Adarsh Patel
Nov 22 at 11:02
|
show 2 more comments
It looks like you can completely remove the threads from this problem and does it work then? What does employee.add do? What is employee?
– matt
Nov 22 at 7:33
yes when we remove the threads it works completely fine, and our employee.add() auto generated all the attributes of employee and add to the list present in different server and send a response from server of Employee type.
– Adarsh Patel
Nov 22 at 8:59
You are using thread-unsafe data structures and operations.
– user207421
Nov 22 at 9:39
You'll need to include the employee.add method to get any help then, also you need to state some explicit quantity that is "incorrect" eg. On the server it is "A" bet the values in employeeList all contain "B".
– matt
Nov 22 at 9:44
Output we received : CompanyId : 0 EmployeeId : 9 CompanyId : 0 EmployeeId : 8 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 1 CompanyId : 0 EmployeeId : 9 CompanyId : 0 EmployeeId : 3 CompanyId : 0 EmployeeId : 0 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 6 Here we can see that there is two Id’s 6 and 9 are repeated.
– Adarsh Patel
Nov 22 at 11:02
It looks like you can completely remove the threads from this problem and does it work then? What does employee.add do? What is employee?
– matt
Nov 22 at 7:33
It looks like you can completely remove the threads from this problem and does it work then? What does employee.add do? What is employee?
– matt
Nov 22 at 7:33
yes when we remove the threads it works completely fine, and our employee.add() auto generated all the attributes of employee and add to the list present in different server and send a response from server of Employee type.
– Adarsh Patel
Nov 22 at 8:59
yes when we remove the threads it works completely fine, and our employee.add() auto generated all the attributes of employee and add to the list present in different server and send a response from server of Employee type.
– Adarsh Patel
Nov 22 at 8:59
You are using thread-unsafe data structures and operations.
– user207421
Nov 22 at 9:39
You are using thread-unsafe data structures and operations.
– user207421
Nov 22 at 9:39
You'll need to include the employee.add method to get any help then, also you need to state some explicit quantity that is "incorrect" eg. On the server it is "A" bet the values in employeeList all contain "B".
– matt
Nov 22 at 9:44
You'll need to include the employee.add method to get any help then, also you need to state some explicit quantity that is "incorrect" eg. On the server it is "A" bet the values in employeeList all contain "B".
– matt
Nov 22 at 9:44
Output we received : CompanyId : 0 EmployeeId : 9 CompanyId : 0 EmployeeId : 8 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 1 CompanyId : 0 EmployeeId : 9 CompanyId : 0 EmployeeId : 3 CompanyId : 0 EmployeeId : 0 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 6 Here we can see that there is two Id’s 6 and 9 are repeated.
– Adarsh Patel
Nov 22 at 11:02
Output we received : CompanyId : 0 EmployeeId : 9 CompanyId : 0 EmployeeId : 8 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 1 CompanyId : 0 EmployeeId : 9 CompanyId : 0 EmployeeId : 3 CompanyId : 0 EmployeeId : 0 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 6 Here we can see that there is two Id’s 6 and 9 are repeated.
– Adarsh Patel
Nov 22 at 11:02
|
show 2 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2fstackoverflow.com%2fquestions%2f53425756%2funpredictable-behavior-in-java-thread-pool-while-capturing-response-for-multiple%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
It looks like you can completely remove the threads from this problem and does it work then? What does employee.add do? What is employee?
– matt
Nov 22 at 7:33
yes when we remove the threads it works completely fine, and our employee.add() auto generated all the attributes of employee and add to the list present in different server and send a response from server of Employee type.
– Adarsh Patel
Nov 22 at 8:59
You are using thread-unsafe data structures and operations.
– user207421
Nov 22 at 9:39
You'll need to include the employee.add method to get any help then, also you need to state some explicit quantity that is "incorrect" eg. On the server it is "A" bet the values in employeeList all contain "B".
– matt
Nov 22 at 9:44
Output we received : CompanyId : 0 EmployeeId : 9 CompanyId : 0 EmployeeId : 8 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 1 CompanyId : 0 EmployeeId : 9 CompanyId : 0 EmployeeId : 3 CompanyId : 0 EmployeeId : 0 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 6 CompanyId : 0 EmployeeId : 6 Here we can see that there is two Id’s 6 and 9 are repeated.
– Adarsh Patel
Nov 22 at 11:02