Synchronized Arraylist between two threads doesn't return the same value












1















I have following code :



import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyThread implements Runnable{

private List<Integer> myList;

public MyThread(List<Integer> list){
this.myList = list;
}
private void updateList(int i){
synchronized (myList) {
myList.add(i);
}
}
@Override
public void run() {
for( int i = 0; i < 1000000;i++){
updateList(i);
}
System.out.println("end: " + myList.size());
}
}
public class MyExecutor {

private List<Integer> taskList = new ArrayList<>();
private void launch(){

ExecutorService executorService= Executors.newFixedThreadPool(10000);
executorService.execute(new MyThread(taskList));
executorService.execute(new MyThread(taskList));
executorService.shutdown();
}
public static void main(String args) {
MyExecutor test = new MyExecutor();
test.launch();
}
}


the output should be : 2000000



I will get different result which means these two threads are replacing each other's value.



I can't figure out where is the problem, tried several modifications on this code but none of them has fixed the problem. (replaced with Vector / added synchronize in constructor / added volatile)



Why doesn't this code work correctly?



Edit



At both thread I expect to get 1000000










share|improve this question

























  • Try printing the size after both threads have finished.

    – Dan W
    Jan 18 at 20:26











  • at both thread I expect to get 1M

    – Vahid hashemi
    Jan 18 at 20:27
















1















I have following code :



import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyThread implements Runnable{

private List<Integer> myList;

public MyThread(List<Integer> list){
this.myList = list;
}
private void updateList(int i){
synchronized (myList) {
myList.add(i);
}
}
@Override
public void run() {
for( int i = 0; i < 1000000;i++){
updateList(i);
}
System.out.println("end: " + myList.size());
}
}
public class MyExecutor {

private List<Integer> taskList = new ArrayList<>();
private void launch(){

ExecutorService executorService= Executors.newFixedThreadPool(10000);
executorService.execute(new MyThread(taskList));
executorService.execute(new MyThread(taskList));
executorService.shutdown();
}
public static void main(String args) {
MyExecutor test = new MyExecutor();
test.launch();
}
}


the output should be : 2000000



I will get different result which means these two threads are replacing each other's value.



I can't figure out where is the problem, tried several modifications on this code but none of them has fixed the problem. (replaced with Vector / added synchronize in constructor / added volatile)



Why doesn't this code work correctly?



Edit



At both thread I expect to get 1000000










share|improve this question

























  • Try printing the size after both threads have finished.

    – Dan W
    Jan 18 at 20:26











  • at both thread I expect to get 1M

    – Vahid hashemi
    Jan 18 at 20:27














1












1








1


2






I have following code :



import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyThread implements Runnable{

private List<Integer> myList;

public MyThread(List<Integer> list){
this.myList = list;
}
private void updateList(int i){
synchronized (myList) {
myList.add(i);
}
}
@Override
public void run() {
for( int i = 0; i < 1000000;i++){
updateList(i);
}
System.out.println("end: " + myList.size());
}
}
public class MyExecutor {

private List<Integer> taskList = new ArrayList<>();
private void launch(){

ExecutorService executorService= Executors.newFixedThreadPool(10000);
executorService.execute(new MyThread(taskList));
executorService.execute(new MyThread(taskList));
executorService.shutdown();
}
public static void main(String args) {
MyExecutor test = new MyExecutor();
test.launch();
}
}


the output should be : 2000000



I will get different result which means these two threads are replacing each other's value.



I can't figure out where is the problem, tried several modifications on this code but none of them has fixed the problem. (replaced with Vector / added synchronize in constructor / added volatile)



Why doesn't this code work correctly?



Edit



At both thread I expect to get 1000000










share|improve this question
















I have following code :



import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyThread implements Runnable{

private List<Integer> myList;

public MyThread(List<Integer> list){
this.myList = list;
}
private void updateList(int i){
synchronized (myList) {
myList.add(i);
}
}
@Override
public void run() {
for( int i = 0; i < 1000000;i++){
updateList(i);
}
System.out.println("end: " + myList.size());
}
}
public class MyExecutor {

private List<Integer> taskList = new ArrayList<>();
private void launch(){

ExecutorService executorService= Executors.newFixedThreadPool(10000);
executorService.execute(new MyThread(taskList));
executorService.execute(new MyThread(taskList));
executorService.shutdown();
}
public static void main(String args) {
MyExecutor test = new MyExecutor();
test.launch();
}
}


the output should be : 2000000



I will get different result which means these two threads are replacing each other's value.



I can't figure out where is the problem, tried several modifications on this code but none of them has fixed the problem. (replaced with Vector / added synchronize in constructor / added volatile)



Why doesn't this code work correctly?



Edit



At both thread I expect to get 1000000







java multithreading






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 18 at 20:28







Vahid hashemi

















asked Jan 18 at 20:06









Vahid hashemiVahid hashemi

2,32774378




2,32774378













  • Try printing the size after both threads have finished.

    – Dan W
    Jan 18 at 20:26











  • at both thread I expect to get 1M

    – Vahid hashemi
    Jan 18 at 20:27



















  • Try printing the size after both threads have finished.

    – Dan W
    Jan 18 at 20:26











  • at both thread I expect to get 1M

    – Vahid hashemi
    Jan 18 at 20:27

















Try printing the size after both threads have finished.

– Dan W
Jan 18 at 20:26





Try printing the size after both threads have finished.

– Dan W
Jan 18 at 20:26













at both thread I expect to get 1M

– Vahid hashemi
Jan 18 at 20:27





at both thread I expect to get 1M

– Vahid hashemi
Jan 18 at 20:27












2 Answers
2






active

oldest

votes


















2














You are getting



end: 1065878
end: 2000000


The first line is from the thread that has finished its job first. It shouldn't be exactly 1M, because several threads are working. It's reasonable to assume that by the time one first thread finishes adding its 1M, the other has added at least one.



The second line is always 2M (as you expected ) due to the synchronised method.




I guess the first thread should execute for the exact number I wanted, no more no less.




Things happened in parallel. The threads were running. Each was trying to invoke updateList: one entered, the others waited. There was no priority on who should be next, so the control over the method was being passed among all the workers in a rather random manner.



I bet you are still thinking of the sequential execution :) One thread runs the whole run method, prints 1M, the other takes a 1M-sized list and adds its portion.



To understand it better, add a print statement



private void updateList(int i) {
synchronized (myList) {
myList.add(i);
System.out.println(Thread.currentThread().getName() + " added " + i);
}
}


and reduce the number of elements to add by a task to, let's say, 10.



pool-1-thread-1 added 0
pool-1-thread-1 added 1
pool-1-thread-1 added 2
pool-1-thread-1 added 3
pool-1-thread-2 added 0
pool-1-thread-2 added 1
pool-1-thread-2 added 2
pool-1-thread-2 added 3
pool-1-thread-1 added 4
pool-1-thread-1 added 5
pool-1-thread-1 added 6
pool-1-thread-1 added 7
pool-1-thread-1 added 8
pool-1-thread-1 added 9
end: 14
pool-1-thread-2 added 4
pool-1-thread-2 added 5
pool-1-thread-2 added 6
pool-1-thread-2 added 7
pool-1-thread-2 added 8
pool-1-thread-2 added 9
end: 20





share|improve this answer


























  • I guess the first thread should execute for the exact number I wanted, no more no less.

    – Vahid hashemi
    Jan 18 at 20:24






  • 3





    It DOES! But the other thread added elements too. First print can give you anything between 1000000 and 200000. Second print will always be exactly 2000000.

    – harogaston
    Jan 18 at 20:27






  • 1





    Minor detail: There are 10 000 threads allowed actually. There are two jobs (Runnables) only though. So only two will ever be used.

    – Mattias Isegran Bergander
    Jan 18 at 20:58



















3















the output should be : 2000000




No, for three reasons:




  1. You are printing two things, so the output won't be a single number.

  2. It prints the size when each thread happens to have added 1000000 things; you know nothing about how much the other thread has done at this point.

  3. You are not accessing the size in a synchronized way, so you are liable to get a non-up to date value.






share|improve this answer


























  • is that what you mean? synchronized (myList) { System.out.println("end: " + myList.size()); }

    – Vahid hashemi
    Jan 18 at 20:15








  • 1





    @Vahidhashemi yes, for the second point

    – Andy Turner
    Jan 18 at 20:17











  • @Vahidhashemi Not accessing size() synchroinously can only give you the result off by 1 am I wrong?

    – harogaston
    Jan 18 at 20:19











  • For the 1st point, wait for both to finish. Can be accomplished with this: ´executorService.awaitTermination(...)´ and then after that print the final size.

    – Mattias Isegran Bergander
    Jan 18 at 20:20






  • 1





    "It prints the size when each thread happens to have added 1000000 things; you know nothing about how much the other thread has done at this point." Well second print should be right though.

    – harogaston
    Jan 18 at 20:20











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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54260756%2fsynchronized-arraylist-between-two-threads-doesnt-return-the-same-value%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You are getting



end: 1065878
end: 2000000


The first line is from the thread that has finished its job first. It shouldn't be exactly 1M, because several threads are working. It's reasonable to assume that by the time one first thread finishes adding its 1M, the other has added at least one.



The second line is always 2M (as you expected ) due to the synchronised method.




I guess the first thread should execute for the exact number I wanted, no more no less.




Things happened in parallel. The threads were running. Each was trying to invoke updateList: one entered, the others waited. There was no priority on who should be next, so the control over the method was being passed among all the workers in a rather random manner.



I bet you are still thinking of the sequential execution :) One thread runs the whole run method, prints 1M, the other takes a 1M-sized list and adds its portion.



To understand it better, add a print statement



private void updateList(int i) {
synchronized (myList) {
myList.add(i);
System.out.println(Thread.currentThread().getName() + " added " + i);
}
}


and reduce the number of elements to add by a task to, let's say, 10.



pool-1-thread-1 added 0
pool-1-thread-1 added 1
pool-1-thread-1 added 2
pool-1-thread-1 added 3
pool-1-thread-2 added 0
pool-1-thread-2 added 1
pool-1-thread-2 added 2
pool-1-thread-2 added 3
pool-1-thread-1 added 4
pool-1-thread-1 added 5
pool-1-thread-1 added 6
pool-1-thread-1 added 7
pool-1-thread-1 added 8
pool-1-thread-1 added 9
end: 14
pool-1-thread-2 added 4
pool-1-thread-2 added 5
pool-1-thread-2 added 6
pool-1-thread-2 added 7
pool-1-thread-2 added 8
pool-1-thread-2 added 9
end: 20





share|improve this answer


























  • I guess the first thread should execute for the exact number I wanted, no more no less.

    – Vahid hashemi
    Jan 18 at 20:24






  • 3





    It DOES! But the other thread added elements too. First print can give you anything between 1000000 and 200000. Second print will always be exactly 2000000.

    – harogaston
    Jan 18 at 20:27






  • 1





    Minor detail: There are 10 000 threads allowed actually. There are two jobs (Runnables) only though. So only two will ever be used.

    – Mattias Isegran Bergander
    Jan 18 at 20:58
















2














You are getting



end: 1065878
end: 2000000


The first line is from the thread that has finished its job first. It shouldn't be exactly 1M, because several threads are working. It's reasonable to assume that by the time one first thread finishes adding its 1M, the other has added at least one.



The second line is always 2M (as you expected ) due to the synchronised method.




I guess the first thread should execute for the exact number I wanted, no more no less.




Things happened in parallel. The threads were running. Each was trying to invoke updateList: one entered, the others waited. There was no priority on who should be next, so the control over the method was being passed among all the workers in a rather random manner.



I bet you are still thinking of the sequential execution :) One thread runs the whole run method, prints 1M, the other takes a 1M-sized list and adds its portion.



To understand it better, add a print statement



private void updateList(int i) {
synchronized (myList) {
myList.add(i);
System.out.println(Thread.currentThread().getName() + " added " + i);
}
}


and reduce the number of elements to add by a task to, let's say, 10.



pool-1-thread-1 added 0
pool-1-thread-1 added 1
pool-1-thread-1 added 2
pool-1-thread-1 added 3
pool-1-thread-2 added 0
pool-1-thread-2 added 1
pool-1-thread-2 added 2
pool-1-thread-2 added 3
pool-1-thread-1 added 4
pool-1-thread-1 added 5
pool-1-thread-1 added 6
pool-1-thread-1 added 7
pool-1-thread-1 added 8
pool-1-thread-1 added 9
end: 14
pool-1-thread-2 added 4
pool-1-thread-2 added 5
pool-1-thread-2 added 6
pool-1-thread-2 added 7
pool-1-thread-2 added 8
pool-1-thread-2 added 9
end: 20





share|improve this answer


























  • I guess the first thread should execute for the exact number I wanted, no more no less.

    – Vahid hashemi
    Jan 18 at 20:24






  • 3





    It DOES! But the other thread added elements too. First print can give you anything between 1000000 and 200000. Second print will always be exactly 2000000.

    – harogaston
    Jan 18 at 20:27






  • 1





    Minor detail: There are 10 000 threads allowed actually. There are two jobs (Runnables) only though. So only two will ever be used.

    – Mattias Isegran Bergander
    Jan 18 at 20:58














2












2








2







You are getting



end: 1065878
end: 2000000


The first line is from the thread that has finished its job first. It shouldn't be exactly 1M, because several threads are working. It's reasonable to assume that by the time one first thread finishes adding its 1M, the other has added at least one.



The second line is always 2M (as you expected ) due to the synchronised method.




I guess the first thread should execute for the exact number I wanted, no more no less.




Things happened in parallel. The threads were running. Each was trying to invoke updateList: one entered, the others waited. There was no priority on who should be next, so the control over the method was being passed among all the workers in a rather random manner.



I bet you are still thinking of the sequential execution :) One thread runs the whole run method, prints 1M, the other takes a 1M-sized list and adds its portion.



To understand it better, add a print statement



private void updateList(int i) {
synchronized (myList) {
myList.add(i);
System.out.println(Thread.currentThread().getName() + " added " + i);
}
}


and reduce the number of elements to add by a task to, let's say, 10.



pool-1-thread-1 added 0
pool-1-thread-1 added 1
pool-1-thread-1 added 2
pool-1-thread-1 added 3
pool-1-thread-2 added 0
pool-1-thread-2 added 1
pool-1-thread-2 added 2
pool-1-thread-2 added 3
pool-1-thread-1 added 4
pool-1-thread-1 added 5
pool-1-thread-1 added 6
pool-1-thread-1 added 7
pool-1-thread-1 added 8
pool-1-thread-1 added 9
end: 14
pool-1-thread-2 added 4
pool-1-thread-2 added 5
pool-1-thread-2 added 6
pool-1-thread-2 added 7
pool-1-thread-2 added 8
pool-1-thread-2 added 9
end: 20





share|improve this answer















You are getting



end: 1065878
end: 2000000


The first line is from the thread that has finished its job first. It shouldn't be exactly 1M, because several threads are working. It's reasonable to assume that by the time one first thread finishes adding its 1M, the other has added at least one.



The second line is always 2M (as you expected ) due to the synchronised method.




I guess the first thread should execute for the exact number I wanted, no more no less.




Things happened in parallel. The threads were running. Each was trying to invoke updateList: one entered, the others waited. There was no priority on who should be next, so the control over the method was being passed among all the workers in a rather random manner.



I bet you are still thinking of the sequential execution :) One thread runs the whole run method, prints 1M, the other takes a 1M-sized list and adds its portion.



To understand it better, add a print statement



private void updateList(int i) {
synchronized (myList) {
myList.add(i);
System.out.println(Thread.currentThread().getName() + " added " + i);
}
}


and reduce the number of elements to add by a task to, let's say, 10.



pool-1-thread-1 added 0
pool-1-thread-1 added 1
pool-1-thread-1 added 2
pool-1-thread-1 added 3
pool-1-thread-2 added 0
pool-1-thread-2 added 1
pool-1-thread-2 added 2
pool-1-thread-2 added 3
pool-1-thread-1 added 4
pool-1-thread-1 added 5
pool-1-thread-1 added 6
pool-1-thread-1 added 7
pool-1-thread-1 added 8
pool-1-thread-1 added 9
end: 14
pool-1-thread-2 added 4
pool-1-thread-2 added 5
pool-1-thread-2 added 6
pool-1-thread-2 added 7
pool-1-thread-2 added 8
pool-1-thread-2 added 9
end: 20






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 18 at 20:59

























answered Jan 18 at 20:22









Andrew TobilkoAndrew Tobilko

27.2k104285




27.2k104285













  • I guess the first thread should execute for the exact number I wanted, no more no less.

    – Vahid hashemi
    Jan 18 at 20:24






  • 3





    It DOES! But the other thread added elements too. First print can give you anything between 1000000 and 200000. Second print will always be exactly 2000000.

    – harogaston
    Jan 18 at 20:27






  • 1





    Minor detail: There are 10 000 threads allowed actually. There are two jobs (Runnables) only though. So only two will ever be used.

    – Mattias Isegran Bergander
    Jan 18 at 20:58



















  • I guess the first thread should execute for the exact number I wanted, no more no less.

    – Vahid hashemi
    Jan 18 at 20:24






  • 3





    It DOES! But the other thread added elements too. First print can give you anything between 1000000 and 200000. Second print will always be exactly 2000000.

    – harogaston
    Jan 18 at 20:27






  • 1





    Minor detail: There are 10 000 threads allowed actually. There are two jobs (Runnables) only though. So only two will ever be used.

    – Mattias Isegran Bergander
    Jan 18 at 20:58

















I guess the first thread should execute for the exact number I wanted, no more no less.

– Vahid hashemi
Jan 18 at 20:24





I guess the first thread should execute for the exact number I wanted, no more no less.

– Vahid hashemi
Jan 18 at 20:24




3




3





It DOES! But the other thread added elements too. First print can give you anything between 1000000 and 200000. Second print will always be exactly 2000000.

– harogaston
Jan 18 at 20:27





It DOES! But the other thread added elements too. First print can give you anything between 1000000 and 200000. Second print will always be exactly 2000000.

– harogaston
Jan 18 at 20:27




1




1





Minor detail: There are 10 000 threads allowed actually. There are two jobs (Runnables) only though. So only two will ever be used.

– Mattias Isegran Bergander
Jan 18 at 20:58





Minor detail: There are 10 000 threads allowed actually. There are two jobs (Runnables) only though. So only two will ever be used.

– Mattias Isegran Bergander
Jan 18 at 20:58













3















the output should be : 2000000




No, for three reasons:




  1. You are printing two things, so the output won't be a single number.

  2. It prints the size when each thread happens to have added 1000000 things; you know nothing about how much the other thread has done at this point.

  3. You are not accessing the size in a synchronized way, so you are liable to get a non-up to date value.






share|improve this answer


























  • is that what you mean? synchronized (myList) { System.out.println("end: " + myList.size()); }

    – Vahid hashemi
    Jan 18 at 20:15








  • 1





    @Vahidhashemi yes, for the second point

    – Andy Turner
    Jan 18 at 20:17











  • @Vahidhashemi Not accessing size() synchroinously can only give you the result off by 1 am I wrong?

    – harogaston
    Jan 18 at 20:19











  • For the 1st point, wait for both to finish. Can be accomplished with this: ´executorService.awaitTermination(...)´ and then after that print the final size.

    – Mattias Isegran Bergander
    Jan 18 at 20:20






  • 1





    "It prints the size when each thread happens to have added 1000000 things; you know nothing about how much the other thread has done at this point." Well second print should be right though.

    – harogaston
    Jan 18 at 20:20
















3















the output should be : 2000000




No, for three reasons:




  1. You are printing two things, so the output won't be a single number.

  2. It prints the size when each thread happens to have added 1000000 things; you know nothing about how much the other thread has done at this point.

  3. You are not accessing the size in a synchronized way, so you are liable to get a non-up to date value.






share|improve this answer


























  • is that what you mean? synchronized (myList) { System.out.println("end: " + myList.size()); }

    – Vahid hashemi
    Jan 18 at 20:15








  • 1





    @Vahidhashemi yes, for the second point

    – Andy Turner
    Jan 18 at 20:17











  • @Vahidhashemi Not accessing size() synchroinously can only give you the result off by 1 am I wrong?

    – harogaston
    Jan 18 at 20:19











  • For the 1st point, wait for both to finish. Can be accomplished with this: ´executorService.awaitTermination(...)´ and then after that print the final size.

    – Mattias Isegran Bergander
    Jan 18 at 20:20






  • 1





    "It prints the size when each thread happens to have added 1000000 things; you know nothing about how much the other thread has done at this point." Well second print should be right though.

    – harogaston
    Jan 18 at 20:20














3












3








3








the output should be : 2000000




No, for three reasons:




  1. You are printing two things, so the output won't be a single number.

  2. It prints the size when each thread happens to have added 1000000 things; you know nothing about how much the other thread has done at this point.

  3. You are not accessing the size in a synchronized way, so you are liable to get a non-up to date value.






share|improve this answer
















the output should be : 2000000




No, for three reasons:




  1. You are printing two things, so the output won't be a single number.

  2. It prints the size when each thread happens to have added 1000000 things; you know nothing about how much the other thread has done at this point.

  3. You are not accessing the size in a synchronized way, so you are liable to get a non-up to date value.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 18 at 20:24

























answered Jan 18 at 20:14









Andy TurnerAndy Turner

82.3k882139




82.3k882139













  • is that what you mean? synchronized (myList) { System.out.println("end: " + myList.size()); }

    – Vahid hashemi
    Jan 18 at 20:15








  • 1





    @Vahidhashemi yes, for the second point

    – Andy Turner
    Jan 18 at 20:17











  • @Vahidhashemi Not accessing size() synchroinously can only give you the result off by 1 am I wrong?

    – harogaston
    Jan 18 at 20:19











  • For the 1st point, wait for both to finish. Can be accomplished with this: ´executorService.awaitTermination(...)´ and then after that print the final size.

    – Mattias Isegran Bergander
    Jan 18 at 20:20






  • 1





    "It prints the size when each thread happens to have added 1000000 things; you know nothing about how much the other thread has done at this point." Well second print should be right though.

    – harogaston
    Jan 18 at 20:20



















  • is that what you mean? synchronized (myList) { System.out.println("end: " + myList.size()); }

    – Vahid hashemi
    Jan 18 at 20:15








  • 1





    @Vahidhashemi yes, for the second point

    – Andy Turner
    Jan 18 at 20:17











  • @Vahidhashemi Not accessing size() synchroinously can only give you the result off by 1 am I wrong?

    – harogaston
    Jan 18 at 20:19











  • For the 1st point, wait for both to finish. Can be accomplished with this: ´executorService.awaitTermination(...)´ and then after that print the final size.

    – Mattias Isegran Bergander
    Jan 18 at 20:20






  • 1





    "It prints the size when each thread happens to have added 1000000 things; you know nothing about how much the other thread has done at this point." Well second print should be right though.

    – harogaston
    Jan 18 at 20:20

















is that what you mean? synchronized (myList) { System.out.println("end: " + myList.size()); }

– Vahid hashemi
Jan 18 at 20:15







is that what you mean? synchronized (myList) { System.out.println("end: " + myList.size()); }

– Vahid hashemi
Jan 18 at 20:15






1




1





@Vahidhashemi yes, for the second point

– Andy Turner
Jan 18 at 20:17





@Vahidhashemi yes, for the second point

– Andy Turner
Jan 18 at 20:17













@Vahidhashemi Not accessing size() synchroinously can only give you the result off by 1 am I wrong?

– harogaston
Jan 18 at 20:19





@Vahidhashemi Not accessing size() synchroinously can only give you the result off by 1 am I wrong?

– harogaston
Jan 18 at 20:19













For the 1st point, wait for both to finish. Can be accomplished with this: ´executorService.awaitTermination(...)´ and then after that print the final size.

– Mattias Isegran Bergander
Jan 18 at 20:20





For the 1st point, wait for both to finish. Can be accomplished with this: ´executorService.awaitTermination(...)´ and then after that print the final size.

– Mattias Isegran Bergander
Jan 18 at 20:20




1




1





"It prints the size when each thread happens to have added 1000000 things; you know nothing about how much the other thread has done at this point." Well second print should be right though.

– harogaston
Jan 18 at 20:20





"It prints the size when each thread happens to have added 1000000 things; you know nothing about how much the other thread has done at this point." Well second print should be right though.

– harogaston
Jan 18 at 20:20


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54260756%2fsynchronized-arraylist-between-two-threads-doesnt-return-the-same-value%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

Callistus III

Plistias Cous

Index Sanctorum