//单线程使用 修改集合 ArrayList arrayList = new ArrayList(); for (int i = 0; i < 100; i++) { arrayList.add(i); } int batch = 10; int size = arrayList.size(); if (0 < size && size <= batch) { List list = arrayList.subList(0, size); System.out.println(">>>>>>>>>>>>:" + JSON.toJSONString(list)); } else { int flag = size % batch == 0 ? size / batch : size / batch + 1; for (int i = 0; i < flag; i++) { if (i == flag - 1) { List list = arrayList.subList(0, arrayList.size()); System.out.println(">>>>>>>>>>>>:" + JSON.toJSONString(list)); arrayList.removeAll(list); } else { List list = arrayList.subList(0, batch); System.out.println(">>>>>>>>>>>>:" + JSON.toJSONString(list)); arrayList.removeAll(list); } } }
//多线程 步长【0,10,20,30,40】修获取 集合上的位置各索引的值 ArrayList arrayList = new ArrayList(); for (int i = 0; i < 10000; i++) { arrayList.add(i); } int batch = 10; int size = arrayList.size(); if (0 < size && size <= batch) { List list = arrayList.subList(0, size); threadPoolExecutor.submit(() -> { print0(list); }); } else { int flag = size % batch == 0 ? size / batch : size / batch + 1; for (int i = 0; i < flag; i++) { List list; if (i == flag - 1) { list = arrayList.subList(i * batch, arrayList.size()); } else { list = arrayList.subList(i * batch, (i + 1) * batch); } threadPoolExecutor.submit(() -> { print0(list); }); } }
下面这个是错误案例:
//多线程并发修改 ArrayList arrayList = new ArrayList(); for (int i = 0; i < 100; i++) { arrayList.add(i); } // ArrayList: // System.arraycopy // System.arraycopy(elementData, 0, a, 0, size); // array.copy 【里面实现使用】浅复制 索引修改后对原数据造成影响 // CopyOnWriteArrayList // es = Arrays.copyOf(es, es.length, Object[].class); CopyOnWriteArrayList copyOnWriteArrayList = new CopyOnWriteArrayList(arrayList); int batch = 10; int size = copyOnWriteArrayList.size(); if (0 < size && size <= batch) { List list = copyOnWriteArrayList.subList(0, size); threadPoolExecutor.submit(() -> { print0(list); }); } else { int flag = size % batch == 0 ? size / batch : size / batch + 1; for (int i = 0; i < flag; i++) { List list; if (i == flag - 1) { list = copyOnWriteArrayList.subList(0, copyOnWriteArrayList.size()); } else { list = copyOnWriteArrayList.subList(0, batch); } threadPoolExecutor.submit(() -> { //每次执行的都是0-9个元素 print0(list); //并发修改异常 copyOnWriteArrayList.removeAll(list); }); } }
//迭代器不会报错 但是不能满足多线程批量处理 CopyOnWriteArrayList copyOnWriteArrayList = new CopyOnWriteArrayList(arrayList); Iterator iterator = copyOnWriteArrayList.iterator(); while (iterator.hasNext()){ Object next = iterator.next(); System.out.println(":::::::::"+next); copyOnWriteArrayList.remove(next); } System.out.println(":::"+JSON.toJSONString(copyOnWriteArrayList));
//多线程并发修改 ArrayList arrayList = new ArrayList(); for (int i = 0; i < 100; i++) { arrayList.add(i); } // ArrayList: // System.arraycopy // System.arraycopy(elementData, 0, a, 0, size); // array.copy 【里面实现使用】浅复制 索引修改后对原数据造成影响【尤其是引用对象】,【建议复制一个新的集合,进行操作,这样两个集合就互不影响了】 //对list操作,就不会影响list0[非引用对象] // ArrayList list=new ArrayList(); // ArrayList list0=new ArrayList(list); // CopyOnWriteArrayList // es = Arrays.copyOf(es, es.length, Object[].class); CopyOnWriteArrayList copyOnWriteArrayList = new CopyOnWriteArrayList(arrayList); int batch = 10; int size = copyOnWriteArrayList.size(); if (0 < size && size <= batch) { List list = copyOnWriteArrayList.subList(0, size); threadPoolExecutor.submit(() -> { print0(list); }); } else { int flag = size % batch == 0 ? size / batch : size / batch + 1; for (int i = 0; i < flag; i++) { List list; if (i == flag - 1) { list = copyOnWriteArrayList.subList(0, copyOnWriteArrayList.size()); } else { list = copyOnWriteArrayList.subList(0, batch); } threadPoolExecutor.submit(() -> { //每次执行的都是0-9个元素 print0(list); //并发修改异常 copyOnWriteArrayList.removeAll(list); }); } }
标签:案例,int,arrayList,list,batch,copyOnWriteArrayList,并发,多线程,size From: https://www.cnblogs.com/wangbiaohistory/p/17578522.html