• 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
    }
    
  • 相关阅读:
    量身打造自己的MyEclipse(多图)
    DevExpress v17.2新版亮点—WPF篇(五)
    DevExpress WPF入门指南:绑定编辑器对话框
    MyEclipse 2017 Stable 2.0发布|附下载
    springmvc常用注解标签详解
    什么是SpringMVC?
    SpringBoot页面渲染
    怎样理解Spring的IOC和AOP?
    LESS 原理,一款css的预处理程序Less的使用
    移动端web app要使用rem实现自适应布局:font-size的响应式
  • 原文地址:https://www.cnblogs.com/fly-book/p/11439145.html
Copyright © 2020-2023  润新知