标签:@param desc lan ati 编写 pack page thread bsp
package www.it.com.test;
/**
* @author wangjie
* @date 2019/11/20 17:10
* @description
* @company 石文软件有限公司
*/
public class User {
private String name;
private Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public User() {
}
public User(String name, Integer id) {
this.name = name;
this.id = id;
}
@Override
public String toString() {
return "User{" +
"name=‘" + name + ‘\‘‘ +
", id=" + id +
‘}‘;
}
}
package www.it.com.test;
import java.util.ArrayList;
import java.util.List;
/**
* @author wangjie
* @date 2019/11/20 17:09
* @description
* @company 石文软件有限公司
*/
public class BranchThread {
public static void main(String[] args) {
//准备数据源
List<User> userList = getInitUser();
//定义每个线程处理多少用户
Integer count = 2;
//根据用户数确定需要多少个线程
List<List<User>> lists = splitList(userList, count);
//将用户传每个线程执行
for (List<User> list:lists) {
UserThread userThread=new UserThread(list);
Thread thread=new Thread(userThread);
//启动线程
thread.start();
}
}
/**
* 初始话数据
* @return
*/
public static List<User> getInitUser() {
ArrayList<User> list = new ArrayList<>();
for (int i = 0; i < 11; i++) {
User user = new User();
user.setId(i);
String name = String.valueOf(i);
user.setName(name);
list.add(user);
}
return list;
}
/**
* 计算出需要多少个线程
* @param list
* @param pageSize
* @param <T>
* @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 www.it.com.test;
import java.util.List;
/**
* @author wangjie
* @date 2019/11/20 17:03
* @description 给11个人分批发送短信
* @company 石文软件有限公司
*/
public class UserThread implements Runnable{
private List<User> userList;
public UserThread(List<User> userList) {
this.userList = userList;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
@Override
public void run() {
userList.forEach(user -> {
System.out.println("线程"+Thread.currentThread().getId()+user.toString());
});
//调用第三方发送短信的接口
}
}
标签:@param desc lan ati 编写 pack page thread bsp
原文地址:https://www.cnblogs.com/wang66a/p/12069285.html