On how ArrayList implements thread security, and how arraylist implements threads

Source: Internet
Author: User

On how ArrayList implements thread security, and how arraylist implements threads

I. Use the synchronized keyword

2. Use Collections. synchronizedList ();

Assume that the code you created is as follows: List <Map <String, Object> data = new ArrayList <Map <String, Object> ();

To solve this thread security problem, you can use Collections. synchronizedList (), for example:

List <Map <String, Object> data = Collections. synchronizedList (new ArrayList <Map <String, Object> ());

The methods used are almost the same as those of ArrayList. For details, refer to the api documentation;

In addition, ArrayList and javaslist are both an implementation under the interface List, which have the same usage, but the places used are a bit different. ArrayList is suitable for a large number of random accesses, the rule list is suitable for table insertion and deletion. Both are non-thread-safe. The solution is the same as above (to avoid thread security, the above method is used, especially the second method, in fact, it is very performance-consuming ).

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.