当前位置:网站首页>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!");
}
}
边栏推荐
- Switch and Router Technology - 32 - Named ACL
- [Embedded open source library] The use of cJSON, an efficient and streamlined json parsing library
- 让你代码越来越高大上的技巧——代码规范,你得知道
- C Language: Practical Debugging Tips
- 金仓数据库 KingbaseGIS 使用手册(6.10. 几何对象操作运算符)
- Switches and routers technologies - 30 - standard acls
- 【嵌入式开源库】MultiButton的使用,简单易用的事件驱动型按键驱动模块
- Switches and routers technology - 24 - configure OSPF single area
- 【无标题】2022年胺基化工艺考试题模拟考试题库及在线模拟考试
- 用白嫖的Adobe正版软件,减少应届毕业生的慢就业、不就业等现象
猜你喜欢

Switch and Router Technology - 32 - Named ACL

Switch and Router Technology-27-OSPF Route Redistribution
![[E-commerce operation] How to formulate a social media marketing strategy?](/img/fe/fc1270e608abbce02f83fcb712e562.jpg)
[E-commerce operation] How to formulate a social media marketing strategy?

Switch and Router Technology - 28 - NSSA Areas for OSPF
![[ARM] rk3399 mounts nfs error](/img/0c/f9f0a2f3850cd55d8bebbee7f898de.png)
[ARM] rk3399 mounts nfs error

Prometheus :(一)基本概念

Application layer protocol - DNS

C statement: data storage

绿盾加密如何顺利切换成IP-Guard加密

Thymeleaf
随机推荐
Redis-使用jedis连接linux中redis服务器失败的解决方案
Kong实现禁止国外IP访问
群晖DS220+ 应用小笔记
Switches and routers technology - 21 - RIP routing protocol
分库分表ShardingSphere-JDBC笔记整理
报表控件Stimulsoft报告中的数据矩阵条形码介绍
网络技能树
Switch and Router Technology-34-Dynamic NAT
[QNX Hypervisor 2.2 User Manual] 10.16 vdev virtio-blk
【ARM】rk3399挂载nfs报错
Four functional interfaces
Some common mysql entry exercises
Redis-数据类型(基本指令、String、List、Set、Hash、ZSet、BitMaps、HyperLogLog、GeoSpatial)/发布和订阅
guava RateLimiter均匀限流
Switch and Router Technology-31-Extended ACL
The use of async (asynchronous) and await
Mysql入门练习
金仓数据库 KingbaseGIS 使用手册(6.10. 几何对象操作运算符)
Redis details
Golden Warehouse Database KingbaseGIS User Manual (6.10. Geometric Object Operation Operator)