java - null is returned while reading last index of an ArrayList -
there 2 threads , b. keeps writing arraylist , b keeps reading it. next code reading arraylist , belongs thread b.
the issue code both of if statements becomes true should not possible. first if there ensure list not read beyond size. sec if not part of actual code. have set verification purposes. can see in output below, size of list greater writenext variable, don't understand why returns null. , yes, if place print statement or a little delay after first if, works fine. please remember there 1 thread writes arraylist, issue should not related synchronization.
public void run() {// thread b public class writetickstofile implements runnable, serializable { int writenext = 0; @override public void run() { while (true) { // read till writenext equal arraylist.size()-1 i.e. lastly index if (writenext < tickdata.getcompleteticklist().size()) { // system.out.println(tickdata.getcompleteticklist().size()+" "+writenext); if (tickdata.getcompleteticklist().get(writenext) == null) { system.out.println("listsize: " + tickdata.getcompleteticklist().size() + " " + "read@index: " + writenext); }// if ends // write element file. writetick(tickdata.getcompleteticklist().get(writenext)); writenext++; }// if ends }// while ends } } output:
number of symbols found: 19 market opening time: 81500 market closing time: 235900 current time: 203931 waiting market open... market opened @ 203931 listsize: 15876 read@index: 15875 listsize: 15877 read@index: 15876
please remember there 1 thread writes arraylist, issue should not related synchronization.
that not true according javadoc arraylist:
note implementation not synchronized. if multiple threads access arraylist instance concurrently, , @ to the lowest degree 1 of threads modifies list structurally, must synchronized externally.
you have multiple threads accessing arraylist concurrently , 1 of them modifying list structurally.
java arraylist
No comments:
Post a Comment