java - Why is arraylist's size not right when multiple threads add elements into it? -
why arraylist's size not right when multiple threads add elements it?
threadcount = 100; list<object> list = new arraylist<>(); (int = 0; < threadcount; i++) {     thread thread = new thread(new mythread(list, countdownlatch));     thread.start(); }  class mythread implements runnable {     // ......      @override     public void run() {         (int = 0; < 100; i++) {             list.add(new object());         }     } } when program done, size of list should 10000. actually, size may 9950, 9965 or other numbers. why?
i know why program may raise indexoutofboundsexception , why there nulls in it, not understand why size wrong.
i not understand why size wrong?
as arraylist not thread-safe 2 threads may add @ same index. 1 thread overwrites.
Comments
Post a Comment