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?










share|improve this question






















  • 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















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?










share|improve this question






















  • 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













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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

















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',
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
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Trompette piccolo

Slow SSRS Report in dynamic grouping and multiple parameters

Simon Yates (cyclisme)