0001.第一个多线程demo--分批处理数据

本文介绍了一个使用Java实现的多线程处理和列表分页的案例。通过创建UserEntity类来存储用户信息,并利用UserThread类在多个线程中遍历和处理这些用户实体。此外,还提供了一个list分页的方法,将用户列表按指定数量分割成多个子列表,以便于在多线程中并行处理,提高了程序的运行效率。
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
}

转载于:https://www.cnblogs.com/fly-book/p/11439145.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值