"Reprint" Java multithreaded read, manipulate list collection

Source: Internet
Author: User

This article was reproduced from: http://blog.csdn.net/wang1989cs/article/details/47663565

Importjava.util.ArrayList;Importjava.util.List;Importorg.apache.commons.lang3.ArrayUtils; Public classTest_4 {/*** Multithreaded Processing list * *@paramData list *@paramthreadnum Number of threads*/     Public synchronized voidHandlelist (list<string> data,intthreadnum) {        intLength =data.size (); intTL = length% Threadnum = = 0? Length/threadnum: (Length/Threadnum + 1);  for(inti = 0; i < threadnum; i++) {            intEnd = (i + 1) *tl; Handlethread Thread=NewHandlethread ("Thread [" + (i + 1) + "]", data, I * tl, end > length?length:end);        Thread.Start (); }    }     classHandlethreadextendsThread {PrivateString ThreadName; PrivateList<string>data; Private intstart; Private intend;  PublicHandlethread (String threadname, list<string> data,intStartintend) {             This. ThreadName =ThreadName;  This. data =data;  This. Start =start;  This. end =end; }          Public voidrun () {//TODO here processing datalist<string> sublist = data.sublist (start, end)/*. Add ("^&*")*/; System.out.println (ThreadName+ "processed" +sublist.size () + "Bar! "); }     }      Public Static voidMain (string[] args) {test_4 Test=NewTest_4 (); //Preparing Datalist<string> data =NewArraylist<string>();  for(inti = 0; i < 6666; i++) {Data.add ("Item" +i); } test.handlelist (Data,5);    System.out.println (arrayutils.tostring (data)); }}

Related article: Java.util.List interface Method sublist () Use precautions

Is there a limit to the list collection store?

A: (Hardware memory factor is not considered) then there should be no upper limit, because its add () method in the Java doc interpretation, there is no capacity constraints. However, one thing, its size () method says returns the number of elements in the list. Returns a integer.max_value if the list contains more than integer.max_value elements. That is, if the capacity exceeds integer.max_value, it cannot be obtained with get (int index), because the maximum parameter range of get is integer.max_value. At this time can only use iterator () to access, but then the application should be meaningless (then go to each element comparison, find what you want, obviously very tired), so, in this sense Integer.max_value can be said to be a list of the upper limit

"Reprint" Java multithreaded read, manipulate list collection

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.