当前位置:网站首页>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!");
}
}
边栏推荐
- Optimization is a kind of habit low starting point is the "standing near the critical"
- [QNX Hypervisor 2.2用户手册]10.15 vdev timer8254
- nodes服务器
- [Untitled] 2022 Amination Process Exam Questions Mock Exam Question Bank and Online Mock Exam
- About CC Attacks
- ESP8266 教程3 — 通过TCP组建局域网并通信
- Network Skill Tree
- Golden Warehouse Database KingbaseGIS User Manual (6.10. Geometric Object Operation Operator)
- 金仓数据库 KingbaseGIS 使用手册(6.10. 几何对象操作运算符)
- Idea essential skills to improve work efficiency
猜你喜欢

代码在线审查(添加网页批注)的实现

Paper Notes: BBN: Bilateral-Branch Network with Cumulative Learning for Long-Tailed Visual Recognition

Linux中安装redis

2021年网络规划设计师下午案例题

2021 Network Planning Designer Afternoon Case Questions

C statement: data storage

C语言题解:谁是凶手!

Four functional interfaces

分库分表之sharding-proxy

用白嫖的Adobe正版软件,减少应届毕业生的慢就业、不就业等现象
随机推荐
Delphi7 learning record - demo example
ESP8266 教程3 — 通过TCP组建局域网并通信
Thymeleaf
Core Data 多线程设计
MFC Interprocess Communication (Shared Memory)
prometheus:(二)监控概述(你永远逃不出我的手掌哈哈)
Paper Notes: Bag of Tricks for Long-Tailed Visual Recognition with Deep Convolutional Neural Networks
网络协议1
shell 脚本编程---入门
Switch and Router Technology - 36-Port Mirroring
Thymeleaf
2022 coal mine gas inspection test, simulation test question and answer
Day38 LeetCode
Application layer protocol - DNS
Switch and Router Technology-34-Dynamic NAT
C语句:数据存储
[QNX Hypervisor 2.2用户手册]10.15 vdev timer8254
FPGA engineer interview questions collection 111~120
JedisLock_Redis分布式锁实现_转载
Optimization is a kind of habit low starting point is the "standing near the critical"