多線程分批處理數據

需求:

  • 給10萬個用戶每一個用戶發一條祝福短信。
  • 爲了提升數程序的效率,請使用多線程技術分批發送據。
  • 每開一個線程,都會佔用CPU資源
  • 服務器(電腦)配置 CPU 核數

創建項目名稱:rain_thread_batch

新建用戶實體類

package com.rain.entity;
public class UserEntity {
    private String userid;
    private String username;
    public UserEntity() {
        
    }
    public UserEntity(String userid, String username) {
        this.userid = userid;
        this.username = 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;
    }
    @Override
    public String toString() {
        return "UserEntity [userid=" + userid + ", username=" + username + "]";
    }
}

創建多線程類UserThread執行發送短信

// 多線程類
class UserSendThread implements Runnable {
    private List<UserEntity> listUser;
    public UserSendThread(List<UserEntity> listUser) {
        this.listUser = listUser;
    }
    @Override
    public void run() {
        for(UserEntity userEntity:listUser) {
            System.out.println(Thread.currentThread().getName()+", "+userEntity.toString());;
        }
        System.out.println();
    }
}

計算分頁工具類

package com.rain.utils;
import java.util.ArrayList;
import java.util.List;
public class ListUtils {
    /**
     * @methodDesc分頁工具
     * @param list
     * @param pageSize
     * @return
     */
    public static <T> List<List<T>> splitList(List<T> list, int pageSize) {
        int listSize = list.size();
        int page = (listSize + (pageSize - 1)) / pageSize;
        List<List<T>>listArray = new ArrayList<List<T>>();
        for (int i = 0; i<page; i++) {
            List<T>subList = new ArrayList<T>();
            for (int j = 0; j<listSize; j++) {
                int pageIndex = ((j + 1) + (pageSize - 1)) / pageSize;
                if (pageIndex == (i + 1)) {
                    subList.add(list.get(j));
                }
                if ((j + 1) == ((j + 1) * pageSize)) {
                    break;
                }
            }
            listArray.add(subList);
        }
        return listArray;
    }
}

初始化數據並實現發送短信

package com.rain;
import java.util.ArrayList;
import java.util.List;
import com.rain.entity.UserEntity;
import com.rain.utils.ListUtils;
public class BatchSms {
    public static void main(String[] args) {
        // 一、初始化數據
        List<UserEntity> initUser = initUser();     
        // 二、定義每一個線程分批發送大小
        int userCount = 2;
        // 三、計算每一個線程須要分配跑的數據
        List<List<UserEntity>> splitList = ListUtils.splitList(initUser,userCount);
        for(int i=0; i<splitList.size(); i++) {
            List<UserEntity> list = splitList.get(i);
//          for(UserEntity u : list) {
//              System.out.println(u.toString());
//          }
            UserSendThread userSendThread = new UserSendThread(list);
        // 四、分配發送
            Thread thread = new Thread(userSendThread,"線程:"+i);
            thread.start();
        }
    }   
    private static List<UserEntity> initUser() {
        List<UserEntity> list = new ArrayList<UserEntity>();
        for (int i = 0; i < 10; i++) {
            list.add(new UserEntity("userId: "+i,"userName: "+i));
        }
        return list;
    }
}
相關文章
相關標籤/搜索