博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java多线程读取、操作List集合
阅读量:4591 次
发布时间:2019-06-09

本文共 1322 字,大约阅读时间需要 4 分钟。

import java.util.ArrayList;

import 
java.util.List;
import 
org.apache.commons.lang3.ArrayUtils;
 
public 
class 
Test_4 {
    
/**
     
* 多线程处理list
     
     
* @param data  数据list
     
* @param threadNum  线程数
     
*/
    
public 
synchronized 
void 
handleList(List<string> data, 
int 
threadNum) {</string>
        
int 
length = data.size();
        
int 
tl = length % threadNum == 
0 
? length / threadNum : (length
                
/ threadNum + 
1
);
 
        
for 
(
int 
i = 
0
; i < threadNum; i++) {
            
int 
end = (i + 
1
) * tl;
            
HandleThread thread = 
new 
HandleThread(
"线程[" 
+ (i + 
1
) + 
"] "
,  data, i * tl, end > length ? length : end);
            
thread.start();
        
}
    
}
 
    
class 
HandleThread 
extends 
Thread {
        
private 
String threadName;
        
private 
List<string> data;</string>
        
private 
int 
start;
        
private 
int 
end;
 
        
public 
HandleThread(String threadName, List<string> data, 
int 
start, 
int 
end) {</string>
            
this
.threadName = threadName;
            
this
.data = data;
            
this
.start = start;
            
this
.end = end;
        
}
 
        
public 
void 
run() {
            
// TODO 这里处理数据
            
data.subList(start, end).add(
"^&*"
);
            
System.out.println(threadName)
        
}
 
    
}
 
    
public 
static 
void 
main(String[] args) {
        
Test_4 test = 
new 
Test_4();
        
// 准备数据
        
List<string> data = 
new 
ArrayList<string>();</string></string>
        
for 
(
int 
i = 
0
; i < 
5000
; i++) {
            
data.add(
"item" 
+ i);
        
}
        
test.handleList(data, 
5
);
        
System.out.println(ArrayUtils.toString(data));
    
}
}

转载于:https://www.cnblogs.com/firstdream/p/8934282.html

你可能感兴趣的文章
maven学习(下)利用Profile构建不同环境的部署包
查看>>
win8自带输入法如何切换全角、半角操作流程
查看>>
TensorFlow windows 安装(base anaconda)
查看>>
Percona XtraDB Cluster集群
查看>>
mybatis学习笔记1--HelloMybatis
查看>>
正则表达式全局匹配网址
查看>>
js多张图片合成一张图,canvas(海报图,将二维码和背景图合并) -----vue
查看>>
前端页面刷新与跳转
查看>>
笔记本链接公司内网(跨网段) ,然后保证wifi
查看>>
Httpd做应用代理
查看>>
如何用Jmter生成合法的手机号
查看>>
Jmeter生成正常的人名
查看>>
Jmeter 做压力测试步骤
查看>>
jmeter生成随机的四位数
查看>>
Jmeter做接口的压力测试
查看>>
sql语句优化的30种方法
查看>>
MyISAM和InnoDB的区别
查看>>
springboot2.0 management.security.enabled无效
查看>>
spring cloud启动zipkin,报错maven依赖jar包冲突 Class path contains multiple SLF4J bindings
查看>>
源发行版8需要目标发行版1.8
查看>>