👉👉請優先查看大佬文章
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class test {
public static void exec(List<String> list) throws InterruptedException {
//一個線程處理300條數據
int count = 300;
//數據集合大小
int listSize = list.size();
//開啟的線程數
int runSize = listSize%count > 0 ? ((listSize/count)+1) : (listSize/count);
//存放每個線程的執行數據
List<String> newlist = null;
//創建一個線程池,數量和開啟線程的數量一樣
ExecutorService executor = Executors.newFixedThreadPool(runSize);
//創建兩個個計數器
CountDownLatch begin = new CountDownLatch(1);
CountDownLatch end = new CountDownLatch(runSize);
//循環創建線程
for (int i = 0; i < runSize; i++) {
//計算每個線程執行的數據
if ((i + 1) == runSize) {
int startIndex = (i * count);
int endIndex = list.size();
newlist = list.subList(startIndex, endIndex);
} else {
int startIndex = (i * count);
int endIndex = (i + 1) * count;
newlist = list.subList(startIndex, endIndex);
}
//線程類
MyThread mythead = new MyThread(newlist, begin, end);
//這里執行線程的方式是調用線程池里的executor.execute(mythead)方法。
executor.execute(mythead);
}
begin.countDown();
end.await();
//執行完關閉線程池
executor.shutdown();
}
//測試
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
//數據越大線程越多
for (int i = 0; i < 3000000; i++) {
list.add("hello" + i);
}
try {
exec(list);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class MyThread implements Runnable {
private List<String> list;
private CountDownLatch begin;
private CountDownLatch end;
//創建個構造函數初始化 list,和其他用到的參數
public MyThread(List<String> list, CountDownLatch begin, CountDownLatch end) {
this.list = list;
this.begin = begin;
this.end = end;
}
@Override
public void run() {
try {
for (int i = 0; i < list.size(); i++) {
//這里還要說一下,由於在實質項目中,當處理的數據存在等待超時和出錯會使線程一直處於等待狀態
//這里只是處理簡單的,
//分批 批量插入
}
//執行完讓線程直接進入等待
begin.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//這里要注意了,當一個線程執行完了計數要減一,不然這個線程會被一直掛起
// end.countDown(),這個方法就是直接把計數器減一的
end.countDown();
}
}
}