当前位置:网站首页>for循环使用多线程优化
for循环使用多线程优化
2022-08-11 05:12:00 【导演1995】
package com.xrq;
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 main(String args[]) throws InterruptedException {
Test test = new Test();
long bt = System.currentTimeMillis();
List<Integer> list = new ArrayList<>();
for(int i=0;i<10;i++){
list.add(i);
}
test.m1(list);
long et2 = System.currentTimeMillis();
System.out.println("[1]耗时:"+(et2 - bt)+ "ms");
Thread thread = new Thread();
long at = System.currentTimeMillis();
test.m2();
long et3 = System.currentTimeMillis();
System.out.println("[2]耗时:"+(et3 - at)+ "ms");
}
public void m1( List<Integer> list) {
ExecutorService pool = Executors.newCachedThreadPool();
CountDownLatch latch = new CountDownLatch(list.size());
for (int i = 0; i < list.size(); i++) {
final int index = i;
Runnable run = new Runnable() {
public void run() {
try {
new Thread().sleep(1000);
//模拟耗时操作
System.out.println("[1]" + Thread.currentThread().getName()+"----"+index);
} catch (Exception e) {
e.printStackTrace();
}
finally {
latch.countDown();
}
}
};
pool.execute(run);
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("[1] done!");
pool.shutdown();
}
public void m2() {
for (int i = 0; i < 10; i++) {
try {
new Thread().sleep(1000); //模拟耗时操作
System.out.println("[2]" + Thread.currentThread().getName()+"----"+i);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("[2] done!");
}
}
边栏推荐
- 什么是三次握手和四次挥手(清晰易懂)
- MySQL事务的概念
- 【嵌入式开源库】MultiButton的使用,简单易用的事件驱动型按键驱动模块
- Use Adobe genuine software for prostitution to reduce the slow employment and non-employment of fresh graduates
- Application layer protocol - DNS
- Idea essential skills to improve work efficiency
- Tips to make your code more and more taller and taller - code specification, you have to know
- 4 Module 3: Literature Reading and Research Methods
- Switches and routers technology - 24 - configure OSPF single area
- In the closing pages/uninstall (unload) sends a request to the server before the document
猜你喜欢
Thymeleaf
Paper Notes: BBN: Bilateral-Branch Network with Cumulative Learning for Long-Tailed Visual Recognition
Redis details
C Language: Practical Debugging Tips
BGP Comprehensive Experiment
Four functional interfaces
Optimization is a kind of habit low starting point is the "standing near the critical"
普林斯顿微积分读本05第四章--求解多项式的极限问题
Paper Notes: Bag of Tricks for Long-Tailed Visual Recognition with Deep Convolutional Neural Networks
玩转mysql之查看mysql版本号
随机推荐
Switch and Router Technology - 32 - Named ACL
Use Adobe genuine software for prostitution to reduce the slow employment and non-employment of fresh graduates
oracle表空间与用户的创建
Smart Pointer Notes
宝塔Linux环境下redis开启多端口
4 Module 3: Literature Reading and Research Methods
[E-commerce operation] How to formulate a social media marketing strategy?
2022 coal mine gas inspection test, simulation test question and answer
4 模块三:文献阅读与研究方法
玩转mysql之查看mysql版本号
Zabbix builds enterprise-level monitoring and alarm platform
BGP综合实验
群晖DS220+ 应用小笔记
How to use svg-icon (svg-sprite-loader plugin)
Prometheus :(一)基本概念
Paper Notes: BBN: Bilateral-Branch Network with Cumulative Learning for Long-Tailed Visual Recognition
普林斯顿微积分读本05第四章--求解多项式的极限问题
Kong implements ban on foreign IP access
HAVE FUN | “SOFA 星球”飞船计划、源码解析活动最新进展
K8s Review Notes 7--K8S Implementation of Redis Standalone and Redis-cluster