任务分线程实现(java)
1、创建一个类,用户存储信息
public class Users { private String userid; private String username; public Users() { } public Users(String userid, String username) { this.userid = userid; this.username = username; } @Override public String toString() { return "Users{" + "userid='" + userid + '\'' + ", 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; } }
2、进行分页,任务分阶段
/** * 分页 * @param list * @param pageSize * @param <T> * @return */ public static <T> List<List<T>> splitList(List<T> list, int pageSize) { int listSize = list.size(); System.out.println(listSize); int page = (listSize + (pageSize - 1)) / pageSize; //分多少页 List<List<T>> listArray = new ArrayList<>(); //用于存放页码,和页面中存放的值 for (int i = 0; i < page; i++) { List<T> subList = new ArrayList<>(); 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; }
3、创建线程,并测试
public class Test { /** * 初始化用户信息 * * @return */ public static List<Users> initUser() { List<Users> listUser = new ArrayList<>(); for (int i = 0; i < 10; i++) { listUser.add(new Users("userId:" + i, "userName:" + i)); } return listUser; } /** * 分页 * @param list * @param pageSize * @param <T> * @return */ public static <T> List<List<T>> splitList(List<T> list, int pageSize) { int listSize = list.size(); System.out.println(listSize); int page = (listSize + (pageSize - 1)) / pageSize; //分多少页 List<List<T>> listArray = new ArrayList<>(); //用于存放页码,和页面中存放的值 for (int i = 0; i < page; i++) { List<T> subList = new ArrayList<>(); 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; } public static void main(String[] args) { //1、初始化数据 List<Users> users = initUser(); //2、每次发送数据 int userCount = 2; //3、分段数据 List<List<Users>> lists = splitList(users, 2); for (int i = 0; i < lists.size(); i++) { List<Users> users1 = lists.get(i); //4、创建线程 new Thread(new Runnable() { @Override public void run() { for (Users listusers : users1) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("name: "+Thread.currentThread().getName()+" "+listusers); } } }).start(); //System.out.println("i: " + i + "---" + users1); } } }
4、测试结果
name: Thread-0 Users{userid='userId:0', username='userName:0'} name: Thread-2 Users{userid='userId:4', username='userName:4'} name: Thread-1 Users{userid='userId:2', username='userName:2'} name: Thread-3 Users{userid='userId:6', username='userName:6'} name: Thread-4 Users{userid='userId:8', username='userName:8'} name: Thread-2 Users{userid='userId:5', username='userName:5'} name: Thread-0 Users{userid='userId:1', username='userName:1'} name: Thread-3 Users{userid='userId:7', username='userName:7'} name: Thread-1 Users{userid='userId:3', username='userName:3'} name: Thread-4 Users{userid='userId:9', username='userName:9'}