0001.第一个多线程demo--分批处理数据
public class UserEntity {
private String userId;
private String userName;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
import java.util.ArrayList;
import java.util.List;
public class UserThread extends Thread {
private List<UserEntity> list;
public UserThread(List<UserEntity> list) {
this.list = list;
}
@Override
public void run() {
for (UserEntity userEntry : list) {
System.out.println("threadName:"+Thread.currentThread().getName()+" userId:"+userEntry.getUserId()+" userName:"+userEntry.getUserName());
//对UserEntity的其它逻辑
}
}
//初始化20个UserEntity
public static List<UserEntity> init(){
List<UserEntity> list = new ArrayList<>();
for (int i = 1; i <= 20; i++) {
UserEntity entity = new UserEntity();
entity.setUserId("userId"+i);
entity.setUserName("userName"+i);
list.add(entity);
}
return list;
}
}
import java.util.ArrayList;
import java.util.List;
public class ListUtils {
/**
* list分页
* @param list
* @param pageSize 每页的长度
* @param <T>
* @return
*/
public static <T> List<List<T>> splitList(List<T> list,int pageSize){
int size = list.size();
// 总页数
int page = (size + (pageSize - 1)) / pageSize;
// 返回的分页数据
List<List<T>> listList = new ArrayList<>();
for (int i = 1; i <= page; i++) {
List<T> subList = new ArrayList<>();
for (int j = 0; j < size; j++) {
int pageIndex = ((j+1)+(pageSize-1))/pageSize;
if (pageIndex==i){
subList.add(list.get(j));
}
//
if ((j+1)==((j+1)*pageSize)){
System.out.println("每页分1一个?");
break;
}
}
listList.add(subList);
}
return listList;
}
}
import java.util.List;
import static com.OtherTest.sendThread.UserThread.init;
public class Main {
public static void main(String[] args){
List<UserEntity> list = init();
int userThreadPage = 5;
List<List<UserEntity>> splitList = ListUtils.splitList(list, userThreadPage);
int size = splitList.size();
for (int i = 0; i < size; i++) {
List<UserEntity> entityList = splitList.get(i);
UserThread userThread = new UserThread(entityList);
userThread.start();
}
}
//threadName:Thread-1 userId:userId6 userName:userName6
//threadName:Thread-1 userId:userId7 userName:userName7
//threadName:Thread-3 userId:userId16 userName:userName16
//threadName:Thread-3 userId:userId17 userName:userName17
//threadName:Thread-3 userId:userId18 userName:userName18
//threadName:Thread-3 userId:userId19 userName:userName19
//threadName:Thread-3 userId:userId20 userName:userName20
//threadName:Thread-0 userId:userId1 userName:userName1
//threadName:Thread-0 userId:userId2 userName:userName2
//threadName:Thread-0 userId:userId3 userName:userName3
//threadName:Thread-0 userId:userId4 userName:userName4
//threadName:Thread-0 userId:userId5 userName:userName5
//threadName:Thread-2 userId:userId11 userName:userName11
//threadName:Thread-2 userId:userId12 userName:userName12
//threadName:Thread-2 userId:userId13 userName:userName13
//threadName:Thread-2 userId:userId14 userName:userName14
//threadName:Thread-2 userId:userId15 userName:userName15
//threadName:Thread-1 userId:userId8 userName:userName8
//threadName:Thread-1 userId:userId9 userName:userName9
//threadName:Thread-1 userId:userId10 userName:userName10
}
0001.第一个多线程demo--分批处理数据的更多相关文章
- 使用java多线程分批处理数据工具类
最近由于业务需要,数据量比较大,需要使用多线程来分批处理,提高处理效率和能力,于是就写了一个通用的多线程处理工具,只需要实现自己的业务逻辑就可以正常使用,现在记录一下 主要是针对大数据量list,将l ...
- C# 实现的多线程异步Socket数据包接收器框架
转载自Csdn : http://blog.csdn.net/jubao_liang/article/details/4005438 几天前在博问中看到一个C# Socket问题,就想到笔者2004年 ...
- Python实战(6)单线程和多线程导入mysql数据对比测试
单线程脚本 导入文件的行数 # wc -l /data/logs/testlog/20120219/testlog1/* 1510503 total # -*- coding: utf-8 -*- # ...
- 秒杀多线程第一篇 多线程笔试面试题汇总 ZZ 【多线程】
http://blog.csdn.net/morewindows/article/details/7392749 系列前言 本系列是本人参加微软亚洲研究院,腾讯研究院,迅雷面试时整理的,另外也加入一些 ...
- 黑马程序员_Java基础:实现多线程对共有数据的同步操作
------- android培训.java培训.期待与您交流! ---------- 实现多线程对共有数据的同步操作,主要涉及到多线程和同步. 虽然都是基础,但是这把刀还是要用熟练,等到使用的时候才 ...
- C#多线程lock解决数据同步
1.代码实例: public class ThreadTest4 { public static void Init() { //多个线程修改同一个值,使用lock锁解决并发 ; i < ; i ...
- split使用和特殊使用(包括截取第一个字符后的数据)
javaScript中关于split()的使用 1.一般使用对一个字符串使用split(),返回一个数组 例子: var testArr = "1,2,3,4,5": var ...
- C# .net 多线程中集合数据同步
from:http://www.cnblogs.com/GavinCome/archive/2008/04/09/1145250.html C# .net 多线程中集合数据同步(转) 集合类通常不是线 ...
- 如果说需要注册数据中心,这样才能使用demo部署数据中心license证需要申请,使用云之间-工作流程......
如果说需要注册数据中心,这样才能使用demo部署数据中心license证需要申请,使用云之间-工作流程......
随机推荐
- eval函数让我忧伤
今天首次接触这个eval函数,让我忧伤了一把.我把当成字符串拼接,结果错得天远地远.大体情况是下面这句代码,一个劲的给我报NameError: name 'qinfeng' is not define ...
- Git的安装及配置
1.Git Git 是一个开源的分布式版本管理工具,可以在你电脑不联网的情况下,只在本地使用的一个版本管理工具,其作用就是可以让你更好的管理你的程序.在你每次的修改代码并提交后,Git 都会将这些记录 ...
- 【Gitlab】git clone http连接,带用户名和密码
test项目在gitlab的http请求的url: http://gitlab.com/test.git 用以下方式请求不需要再输入用户名和密码 git clone http://username: ...
- 【HDOJ6610】Game(序列带修莫队)
题意:有n堆石子,第n堆有a[i]个,A先选择一个范围[L,R],B选择一个子区间[l,r],之后照nim游戏的规则进行 现在有询问与操作 每次询问B在给定的[L,R]内有多少种子区间的取法使得A必胜 ...
- [CSP-S模拟测试74]题解
A.梦境 如果不用去重一定要用Multiset……挂30分算是出题人手下留情了. 贪心.把点排序,区间按右端点递增排序.依次考虑每个区间,取能选的最靠左的点即可.multiset维护. #includ ...
- 【Java架构:持续交付】一篇文章搞掂:持续交付理论
一.持续集成.持续交付.DevOps概念,关系等 持续集成(Continuous integration/CI) 持续交付(Continuous delivery/CD) 持续部署() 持续 (Con ...
- (转)JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解
转:https://my.oschina.net/feichexia/blog/196575?p=3 现实企业级Java开发中,有时候我们会碰到下面这些问题: OutOfMemoryError,内存不 ...
- ubuntu16.04 常用软件
解决安装包依赖问题 更新数据库源 sudo apt-get update sudo apt-get -f -y install guake Terminal 作用:就是一个终端,按F12就出现,再按就 ...
- ROM、RAM、DRAM、SRAM、FLASH的区别?
在学习单片机的时候经常会被这些东西搞晕掉,什么ROM RAM FLASH EEPROM 等等......为了不被搞晕,做个笔记,不记得的时候过来看看. 下面是我在网上找的资料: ROM和RAM指的都是 ...
- java并发编程笔记(八)——死锁
java并发编程笔记(八)--死锁 死锁发生的必要条件 互斥条件 进程对分配到的资源进行排他性的使用,即在一段时间内只能由一个进程使用,如果有其他进程在请求,只能等待. 请求和保持条件 进程已经保持了 ...