`

多线程 ExecutorService CountDownLatch

    博客分类:
  • Java
阅读更多
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;
}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics