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--分批处理数据的更多相关文章

  1. 使用java多线程分批处理数据工具类

    最近由于业务需要,数据量比较大,需要使用多线程来分批处理,提高处理效率和能力,于是就写了一个通用的多线程处理工具,只需要实现自己的业务逻辑就可以正常使用,现在记录一下 主要是针对大数据量list,将l ...

  2. C# 实现的多线程异步Socket数据包接收器框架

    转载自Csdn : http://blog.csdn.net/jubao_liang/article/details/4005438 几天前在博问中看到一个C# Socket问题,就想到笔者2004年 ...

  3. Python实战(6)单线程和多线程导入mysql数据对比测试

    单线程脚本 导入文件的行数 # wc -l /data/logs/testlog/20120219/testlog1/* 1510503 total # -*- coding: utf-8 -*- # ...

  4. 秒杀多线程第一篇 多线程笔试面试题汇总 ZZ 【多线程】

    http://blog.csdn.net/morewindows/article/details/7392749 系列前言 本系列是本人参加微软亚洲研究院,腾讯研究院,迅雷面试时整理的,另外也加入一些 ...

  5. 黑马程序员_Java基础:实现多线程对共有数据的同步操作

    ------- android培训.java培训.期待与您交流! ---------- 实现多线程对共有数据的同步操作,主要涉及到多线程和同步. 虽然都是基础,但是这把刀还是要用熟练,等到使用的时候才 ...

  6. C#多线程lock解决数据同步

    1.代码实例: public class ThreadTest4 { public static void Init() { //多个线程修改同一个值,使用lock锁解决并发 ; i < ; i ...

  7. split使用和特殊使用(包括截取第一个字符后的数据)

    javaScript中关于split()的使用 1.一般使用对一个字符串使用split(),返回一个数组 例子:   var testArr = "1,2,3,4,5": var ...

  8. C# .net 多线程中集合数据同步

    from:http://www.cnblogs.com/GavinCome/archive/2008/04/09/1145250.html C# .net 多线程中集合数据同步(转) 集合类通常不是线 ...

  9. 如果说需要注册数据中心,这样才能使用demo部署数据中心license证需要申请,使用云之间-工作流程......

    如果说需要注册数据中心,这样才能使用demo部署数据中心license证需要申请,使用云之间-工作流程......

随机推荐

  1. Spring---数据缓存Cache

    1.Spring缓存支持 1.1.Spring定义了org.springframework.cache.CacheManager类.org.springframework.cache.Cache类接口 ...

  2. Mac下通过npm安装webpack 、vuejs,以及引入jquery、bootstrap等(初稿)

    前言: 初次接触前端开发技术,一些方向都是在同事的指引和自己的探索后,跑了个简易web,迈入全栈系列.由于是事后来的文章,故而暂只是杂记,写的粗略且不清晰,后续将补之. 主要参考文档: http:// ...

  3. rem字体+百分比布局表格

    效果图: 上源码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  4. spring微服务(顺序由简入难易于理解)

    一.为微服务应用增加健康监控 1.在 build.gradle 文件 dependencies 属性中增加 compile('org.springframework.boot:spring-boot- ...

  5. Git使用包括切换分支

  6. NAGIOS(网络监视工具)

    Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机,路由器等网络设备,打印机等.在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员 ...

  7. PHP远程DoS漏洞深入分析及防护方案

    PHP远程DoS漏洞 4月3日,有人在PHP官网提交PHP 远程DoS漏洞(PHP Multipart/form-data remote dos Vulnerability),代号69364.由于该漏 ...

  8. PHP 常用自定义函数

    模拟 POST.GET 请求 /** * 模拟post进行url请求 * @param string $url * @param string $param */ protected function ...

  9. CentOS7 防火墙Firewall常用命令

    1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld  停止: systemctl disab ...

  10. Linux随笔 - Linux LVM逻辑卷配置过程详解[转载]

    许多Linux使用者安装操作系统时都会遇到这样的困境:如何精确评估和分配各个硬盘分区的容量,如果当初评估不准确,一旦系统分区不够用时可能不得不备份.删除相关数据,甚至被迫重新规划分区并重装操作系统,以 ...