当前位置:网站首页>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!");
}
}
边栏推荐
- 2022 Quality Officer-Civil Construction Direction-General Basic (Quality Officer) Exam Mock 100 Questions and Online Mock Exam
- 关于CC 攻击
- Switches and routers technology - 24 - configure OSPF single area
- [E-commerce operation] How to formulate a social media marketing strategy?
- Internet Protocol 1
- 02. Fold hidden text
- Tips to improve your productivity, you have to know - Navitcat shortcuts
- @Resource和@Autowired的区别
- 元宇宙社交应用,靠什么吸引用户「为爱发电」?
- Golden Warehouse Database KingbaseGIS User Manual (6.10. Geometric Object Operation Operator)
猜你喜欢
leetcode 9. Palindromic Numbers
ESP8266 教程3 — 通过TCP组建局域网并通信
Switches and routers technologies - 30 - standard acls
Linux中安装redis
代码在线审查(添加网页批注)的实现
Weekly recommended short video: your commonly used Polaroid, its predecessor turned out to be like this!
Redis-数据类型(基本指令、String、List、Set、Hash、ZSet、BitMaps、HyperLogLog、GeoSpatial)/发布和订阅
shell 脚本编程---入门
Unity WebGL RuntimeError: integer overflow
[E-commerce operation] How to formulate a social media marketing strategy?
随机推荐
[No 2022 Shanghai Security Officer A Certificate Exam Question Bank and Mock Exam
【嵌入式开源库】MultiButton的使用,简单易用的事件驱动型按键驱动模块
MySQL必知必会(初级篇)
宝塔Linux环境下redis开启多端口
form form submission database Chinese becomes a question mark
实战noVNC全过程操作(包含遇到的问题和解决)
在 关闭页面/卸载(unload)文档 之前向服务器发送请求
2022 building welder (building a special type of work) examination questions and simulation test
批量修改数据库等视频文件名称
Prometheus :(一)基本概念
Core Data 多线程设计
Development Tools Lecture 7: Alibaba Cloud Log Query and Analysis
【无2022上海市安全员A证考试题库及模拟考试
Project Practice Lecture 27: Application of Status Mode in Duplicate Brands
[ARM] rk3399 mounts nfs error
[Untitled] 2022 Amination Process Exam Questions Mock Exam Question Bank and Online Mock Exam
Switch and Router Technology-29-OSPF Virtual Link
Weekly recommended short video: your commonly used Polaroid, its predecessor turned out to be like this!
redis集群模式--解决redis单点故障
4 Module 3: Literature Reading and Research Methods