多线程 ExecutorService CountDownLatch

package com.hbw.model;

public class User {

public User(){}
public User(int id,String name){
this.id=id;
this.name=name;

}
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}


}


package com.hbw.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.hbw.model.User;

public class UserDao {

private static int recordCount=899;
private int pageSize=100;
private int page=1;

private static List<User> list=Collections.synchronizedList(new ArrayList<User>());
static{
for (int i = 001; i <= recordCount; i++) {
User user=new User(i,"姓名:"+i);
list.add(user);
}
}

public List<User> getUserData(int pageSize, int page){
this.pageSize=pageSize;
this.page=page;
if(pageSize*(page+1)>list.size()){
return list.subList(page*100, list.size());
}else{
return list.subList(page*100, pageSize*(page+1));
}
}
public int getPageCount() {
return (recordCount == 0) ? 1 : ((recordCount % pageSize == 0) ? (recordCount / pageSize)
: (recordCount / pageSize) + 1);
}

public static void main(String[] args) {
UserDao dao=new UserDao();
List<User> list=dao.getUserData(100, 4);
for (User user : list) {
System.out.println(user.getId()+"____"+user.getName());
}
}
}


package com.hbw.thread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import com.hbw.dao.UserDao;
import com.hbw.model.User;


public class ExecutorServiceDemo {
// 默认的并发级别
private final static int DEFAULT_CONCURRENCY_LEVEL = 5;
// 是否已经加载到最后一页
private volatile boolean isLastPage;
// 当前页
private AtomicInteger currentPage = new AtomicInteger(1);
// 总数
private static AtomicInteger count = new AtomicInteger(0);

private static ExecutorService executorService = Executors.newFixedThreadPool(DEFAULT_CONCURRENCY_LEVEL);

public static List<User> tempList= Collections.synchronizedList(new ArrayList<User>());

public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(DEFAULT_CONCURRENCY_LEVEL);

long startTime=System.currentTimeMillis();
ExecutorServiceDemo test=new ExecutorServiceDemo();
test.getData(latch);
latch.await();
executorService.shutdown();
long endTime=System.currentTimeMillis(); //获取结束时间
System.err.println(tempList.size());
System.err.println("程序运行时间: "+((endTime-startTime)/1000)+"s:"+count.get());
}

public int getData(final CountDownLatch latch) throws InterruptedException{


final UserDao dao=new UserDao();
System.out.println(dao.getPageCount());
for(int i = 0; i < DEFAULT_CONCURRENCY_LEVEL; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
while(!isLoadLast()) {
int pageNum = currentPage.getAndIncrement();
if(pageNum > dao.getPageCount()) {
isLastPage = true;
continue ;
}

List<User> list=dao.getUserData(100,pageNum-1);

for (User u : list) {
System.out.println(u.getId()+"_____"+u.getName());
count.incrementAndGet();
tempList.add(u);
}
}
latch.countDown();
}
});
}
return count.get();

}

public boolean isLoadLast() {
return isLastPage;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值