Unpredictable behavior in Java Thread Pool while capturing response for multiple threads
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
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 '18 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 '18 at 8:59
You are using thread-unsafe data structures and operations.
– user207421
Nov 22 '18 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 '18 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 '18 at 11:02
|
show 2 more comments
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 '18 at 7:23
Adarsh PatelAdarsh Patel
73
73
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 '18 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 '18 at 8:59
You are using thread-unsafe data structures and operations.
– user207421
Nov 22 '18 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 '18 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 '18 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 '18 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 '18 at 8:59
You are using thread-unsafe data structures and operations.
– user207421
Nov 22 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 8:59
You are using thread-unsafe data structures and operations.
– user207421
Nov 22 '18 at 9:39
You are using thread-unsafe data structures and operations.
– user207421
Nov 22 '18 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 '18 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 '18 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 '18 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 '18 at 11:02
|
show 2 more comments
0
active
oldest
votes
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',
autoActivateHeartbeat: false,
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
});
}
});
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
0
active
oldest
votes
0
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.
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 '18 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 '18 at 8:59
You are using thread-unsafe data structures and operations.
– user207421
Nov 22 '18 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 '18 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 '18 at 11:02