zoukankan      html  css  js  c++  java
  • 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
    }
    
  • 相关阅读:
    js 实现长按效果(类似安卓的)
    Java编程思想学习笔记(一)
    中文自然语言处理(NLP)(五)应用HanLP分词模块进行分词处理
    中文自然语言处理(NLP)(四)运用python二维字典和jieba实现词频的统计
    中文自然语言处理(NLP)(三)运用python jieba模块计算知识点当中关键词的词频
    中文自然语言处理(NLP)(二)python jieba模块的进一步学习和xlrd模块
    中文自然语言处理(NLP)(一)python jieba模块的初步使用
    正则表达式(几个例子)
    用户登陆界面(jquery)
    一个简单的注册页面,基于JS
  • 原文地址:https://www.cnblogs.com/fly-book/p/11439145.html
Copyright © 2011-2022 走看看