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