博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
任务分线程实现(java)
阅读量:4980 次
发布时间:2019-06-12

本文共 3784 字,大约阅读时间需要 12 分钟。

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 
* @return */ public static
List
> splitList(List
list, int pageSize) { int listSize = list.size(); System.out.println(listSize); int page = (listSize + (pageSize - 1)) / pageSize; //分多少页 List
> listArray = new ArrayList<>(); //用于存放页码,和页面中存放的值 for (int i = 0; i < page; i++) { List
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
initUser() { List
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
* @return */ public static
List
> splitList(List
list, int pageSize) { int listSize = list.size(); System.out.println(listSize); int page = (listSize + (pageSize - 1)) / pageSize; //分多少页 List
> listArray = new ArrayList<>(); //用于存放页码,和页面中存放的值 for (int i = 0; i < page; i++) { List
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 = initUser(); //2、每次发送数据 int userCount = 2; //3、分段数据 List
> lists = splitList(users, 2); for (int i = 0; i < lists.size(); i++) { List
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'}

 

转载于:https://www.cnblogs.com/karrya/p/10951805.html

你可能感兴趣的文章
java学习——异常处理
查看>>
实验10: RIP
查看>>
ArcGIS Server 10.1发布要素服务(FeatureLayer server)时遇到的数据库注册问题
查看>>
java 动手动脑
查看>>
手动删除数据库表,并注释创建的model后,同步数据库命令操作
查看>>
聊一聊JS的原型链之高级篇
查看>>
Ubuntu 11.10 禁用关机提示
查看>>
ListView实现点击事件以及总结
查看>>
设计模式使用方法总结
查看>>
ThreadLocal的应用与实现原理
查看>>
Ubuntu用sudo apt-get update出错:E: Problem executing scripts APT::Update::Post-Invoke-Success
查看>>
异常处理与内建模块
查看>>
Linux系统下搭建FTP/SFTP服务器
查看>>
获取手机信息(UIDevice、NSBundle、NSLocale)
查看>>
函数sprintf真好用
查看>>
网络欺诈防范
查看>>
JAVA入门——Generic/泛型
查看>>
【转】iOS笔记-自定义控件(OC)
查看>>
fiddler手机安装证书报错 无法安装该证书 因为无法读取该证书文件
查看>>
css框架,一把锋利的剑
查看>>