对于必须在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("完成");
}
执行结果:
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("完成");
}
执行结果:
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("结束");
}
执行结果:
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();
}
}
}
执行结果:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章