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证需要申请,使用云之间-工作流程......
随机推荐
- 厉害了!新AI人工智能研究令人大开眼界
AI人工智能有很多方法可以操纵照片,让您看起来更好看,消除红眼或镜头眩光等等.但到目前为止,眨眼已经证明了一个顽强的快照对手. 这远远不是智能“绘画中”的唯一例子,因为当一个程序用它认为属于的地方填充 ...
- shell编程之基础知识1
1.shell脚本的基本格式 #!bin/bash ->看到这个就是shell脚本 #filename:test.sh ->脚本名称 #auto echo hello world -& ...
- appium---学习
一直想学但是没有时间,今天看到个不错的链接保存一下. 学习链接:http://www.testclass.net/appium_base/appium-base-summary
- Linux任务计划at
Linux任务计划at 一Linux任务计划介绍 Linux任务计划.周期性任务执行at:未来的某时间点执行一次任务batch:系统自行选择空闲时间去执行此处指定的任务cron:周期性运行某任务 二a ...
- ECS运维:操作系统有异常?诊断日志来帮忙!
云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新.阿里云使用严格的IDC标准.服务器准入标准 ...
- c#消息窗体
C#模拟弹出窗体系统菜单介绍 using System.Runtime.InteropServices; ; ; ; ; ; ; const uint TPM_VCENTERALIGN = 0x10; ...
- HDU 4812 (点分治)
题目:https://vjudge.net/contest/307753#problem/E 题意:给你一颗树,树上每个点都有个权值,现在问你是否存在 一条路径的乘积 mod 1e6+3 等于 k的 ...
- pytest_用例运行级别_函数级
''' 函数级(setup_function/teardown_function只对函数用例生 效(不在类中)在类中是用该方法不生效 ''' import pytest def setup_mod ...
- arm可以干什么
ARM开发可以控制各种电机.arm性能很强 ,内存更大, c语言当然可以.ARM是32位的,单片机是8位的,运行速度快很多,最关键的是可以跑操作系统.控制部分的内容ARM当然可以胜任,而且ARM的资源 ...
- java ee项目用gradle依赖打包
plugins { id 'java' id 'eclipse' id 'idea' id 'application' } //mainClassName = ConnectionElasticSea ...