ArrayList concurrency Add () may appear array subscript out of bounds exception | 10+10<20__java

Source: Internet
Author: User

Suppose you know the implementation of the Add () inside the ArrayList. Do not know that we will review in detail, two-step operation, Step1 Check array capacity, Step2 plug value and will size++.

--array subscript out of bounds

Suppose there are 2 threads operating the same ArrayList al, and the array's capacity is just as good as a value. Thread1 execution of Add () Step1 completed, Step2 has not started to be suspended, then Thread1 "think" array still have position can plug the value, and then sleep at ease; Thread2 executes add (), stores an element and will size++, and + The size after 1 is already larger than the array current capacity. Then Thread1 wakes up and executes, because it has checked the array capacity before going to bed, so directly executes the STEP2, directly to the place that the value is labeled size, so it crosses the line.

--10+10<20 (two threads each ADD10 element, finally size<20)

Suppose there are 2 threads operating the same ArrayList. Case one: Thread1 executes Add () to step2 the plug value completes but size++ is suspended when it is not started, Thread2 executes add (), stores an element and size++. The problem is, because the THREAD1 plug value will not be size+1, so Thread2 into the position of the element and Thread1 the same position, Thread1 the value is covered. Then Thread1 will size+1. In this case, the consistency of size is not corrupted, but the array has a location where null occurs.  Scenario Two: If the front Thread1 is suspended (the value of size has been taken only +1 | The value of size has been fetched and +1 has not yet been written back to al. That is, the size++ completes half (the Java Self-atomic operation), then the size of the Thread1 is actually out of date when it wakes up, at which point the Size field (property) is assigned to Al (+1 again | directly). The size value is not the same as expected. Of course, if there's no other problem, the size value corresponds to the number of elements that the array actually saves, but some of the data that should be stored in the middle is lost.


The essential:

1, Thread1 is suspended that moment of execution of the position is not the same;

2, size is a two-thread public variable;

3. Self-increasing non-atomic operation;

4, can not describe ...

(The value of size has been taken only +1 | has been taken to the value of size and +1 but has not been written back to Al, I do not know how to split into a few atoms of the operation, the back to know to change.

Finish.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.