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. CF 49E Common ancestor

    传送门 模拟赛T1就自闭了( 才不会说我是去刚T2了来着 感觉非常暴力的一个题? 出题人良心开大数据范围 n=100 还是原来的n^4*26算法我也是自闭了 不过貌似跑不到n^4? 真·大力出奇迹 一 ...

  2. vagrant(二)配置文件vagrantfile详解 以及安装php、nginx、mysql

    上一篇文章完整的讲叙了如何安装一个vagrant的环境.这里主要说一说vagrant的配置文件Vagrantfile. 一 配置详解 在我们的开发目录下有一个文件Vagrantfile,里面包含有大量 ...

  3. nyoj 78:圈水池 【凸包入门】

    题目链接 将所有点按从左至右顺序排序,然后将所有点先从左到右扫描再从右到左扫描,逐渐将凸包轮廓“勾勒”出来 (凸包轮廓满足,轮廓上连续的三个点按先后顺序给出的话呈逆时针方向) 最后删去一个重复的起(终 ...

  4. docker 报错端口被占用 sqlserver 占用80端口

    本想开启个容器,却发现有程序居然占用了我的80端口 C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response fr ...

  5. solr测试用的配置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  6. 趣头条基于 Flink 的实时平台建设实践

    本文由趣头条实时平台负责人席建刚分享趣头条实时平台的建设,整理者叶里君.文章将从平台的架构.Flink 现状,Flink 应用以及未来计划四部分分享. 一.平台架构 1.Flink 应用时间线 首先是 ...

  7. spring-boot整合mongodb的案例

    1.简介 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品 ...

  8. php简易分词

    http://www.xunsearch.com/ 示例 http://www.xunsearch.com/scws/demo/v48.php

  9. JDK1.8 动态代理机制及源码解析

    动态代理 a) jdk 动态代理 Proxy, 核心思想:通过实现被代理类的所有接口,生成一个字节码文件后构造一个代理对象,通过持有反射构造被代理类的一个实例,再通过invoke反射调用被代理类实例的 ...

  10. mysql 一条sql完成saveOrUpdate 存在即更新

    关键字 on duplicate key update <pre name="code" class="sql"> insert into tabl ...