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证需要申请,使用云之间-工作流程......
随机推荐
- Unit Testing of Classes in Java
Every class can have a main method. That is a handy trick for unit testing of classes. For example, ...
- JAVA学习笔记--赋值(“=”)
参考来源:<java编程思想(第四版)> 见第三章3.4节 基本数据类型存储了实际的数值,并非指向一个对象的引用,故其赋值,就是直接将一个地方的内容复制到了另一个地方.例如,对基本数据类型 ...
- leetcode-15双周赛-1286-字母组合迭代器
题目描述: 方法: class CombinationIterator: def __init__(self, characters: str, combinationLength: int): se ...
- DataInput接口说明及其实现类
一. DataInput接口 DataInput接口提供了一系列的方法从二进制流中读取字节,并将读取出来的字节转换成任意的java基本类型,包括转换成UTF-8类型的字符串. 该接口中主要方法介绍如下 ...
- 集训队8月3日(A*+IDA*)
刷题数:4 今天看书看了A*与IDA*,算法竞赛入门经典124~133页. 先说一下看书后对知识点的认识,A*算法就是设计一个估价函数,附加到其优先队列的权值比较中,然后还是得到目标状态的解.值得一提 ...
- LintCode之删除排序链表中的重复元素
题目描述: 我的代码: /** * Definition for ListNode * public class ListNode { * int val; * ListNode next; * Li ...
- springBoot JPA PageAble分页查询出错,PropertyReferenceException: No property creation found for type
PropertyReferenceException: No property creation found for type @RequestParam(required = false, defa ...
- 用 Flask 来写个轻博客 (36) — 使用 Flask-RESTful 来构建 RESTful API 之五
目录 目录 前文列表 PUT 请求 DELETE 请求 测试 对一条已经存在的 posts 记录进行 update 操作 删除一条记录 前文列表 用 Flask 来写个轻博客 (1) - 创建项目 用 ...
- Neural Network and Artificial Neural Network
神经网络的基本单元为神经元neuron,也称为process unit,可以做一些基本的运算操作. 人脑和动物大脑的发育,依赖于经验的积累和学习.神经网络就是一个用来仿照人脑进行学习的机器,其包含 ...
- js的几个特殊的运算符略解
js运算符的一些特殊应用及使用技巧. 1. 是否包含指定字符: ~ ~"str1".indexOf("str2") 含义为:str1 被查找的字符串 str2 ...