1.對for循環內數據啓用多線程執行,主線程與子線程無先後順序
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 5; i++) {
ThreadUtil.execAsync(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程" + Thread.currentThread().getName() + "執行完");
});
System.out.println("第" + i + "個線程");
}
System.out.println("完成");
}
執行結果:
- 對for循環內數據啓用多線程執行,主線程在所有子線程執行完成之後執行
public static void main(String[] args) throws InterruptedException {
//初始化線程數量
CountDownLatch countDownLatch = ThreadUtil.newCountDownLatch(5);
for (int i = 0; i < 5; i++) {
ThreadUtil.execute(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程" + Thread.currentThread().getName() + "執行完");
//調用線程計數器-1
countDownLatch.countDown();
});
System.out.println("第" + i + "個線程");
}
//喚醒主線程
countDownLatch.await();
System.out.println("完成");
}
執行結果:
- 對for循環內數據啓用多線程執行,主線程在所有子線程執行完成之後執行
public static void main(String[] args) throws InterruptedException {
// 線程個數
int N = 10;
// 實例化一個倒計數器,N指定計數個數
CountDownLatch countDownLatch = new CountDownLatch(N);
for (int i = 0; i < N; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
System.out.println("子線程" + Thread.currentThread().getName() + "休眠結束");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 計數減一
countDownLatch.countDown();
}
}
}).start();
}
// 阻塞,等待當計數減到0時,執行後面的代碼
countDownLatch.await();
System.out.println("結束");
}
執行結果:
4. JAVA多線程10個線程處理1000個數據
public static void main(String[] args) throws Exception {
List<Integer> idList = new ArrayList<>();
for (int i = 1; i <= 1000; i++) {
idList.add(i);
}
int threadNum = 10;
ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
CountDownLatch countDownLatch = new CountDownLatch(threadNum);
int perSize = idList.size() / threadNum;
// 定義接受數據集合 多線程情況下,使用線程安全集合
List<Integer> resultList = Collections.synchronizedList(new ArrayList());
for (int i = 0; i < threadNum; i++) {
MultiThread thread = new MultiThread();
thread.setIdList(idList.subList(i * perSize, (i + 1) * perSize));
thread.setCountDownLatch(countDownLatch);
thread.setResultList(resultList);
executorService.submit(thread);
}
countDownLatch.await();
executorService.shutdown();
// 查看結果
System.out.println(resultList.size());
System.out.println(resultList.stream().sorted().collect(Collectors.toList()));
}
}
class MultiThread extends Thread {
private List<Integer> idList;
private CountDownLatch countDownLatch;
private List<Integer> result;
public void setResultList(List<Integer> result) {
this.result = result;
}
public void setIdList(List<Integer> idList) {
this.idList = idList;
}
public void setCountDownLatch(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
// 數據處理
for (Integer integer : idList) {
if (integer % 2 == 0) {
result.add(integer);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (countDownLatch != null) {
countDownLatch.countDown();
}
}
}
執行結果: