上次使用Callable,试图用ExecutorService开启十个线程查询数据,我今天试了下,发现启动后事实上运行的都是主线程,下面上一下代码和结果:
测试类:
package com.next.jiangzh.film.dao.mapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
@RunWith(SpringRunner.class)
@SpringBootTest
public class FilmFieldTMapperTest2 {
@Autowired
private NextUserMapper nextUserMapper;
@Test
public void describeFieldListTest() throws InterruptedException, ExecutionException{
List<List> result = new ArrayList<>();//返回结果
long startTime = System.currentTimeMillis();
//查询数据库总数量
int count = nextUserMapper.count();
System.out.println(count);
int num = 5000;//一次查询多少条
//需要查询的次数
int times = count / num;
if (count % num != 0) {
times = times + 1;
}
//开始页数 连接的是Mysql的数据库 封装的分页方式 我的是从0开始
int start = 0;
//定义固定长度的线程池 防止线程过多
ExecutorService executorService = Executors.newFixedThreadPool(15);
//Callable用于产生结果
//List<Future<List>> futureList = new ArrayList<Future<List>>();
List<Callable<List>> futureList = new ArrayList<Callable<List>>();
for (int i = 0; i < times; i++) {
//Future<List> qfe = executorService.submit(new test(start,num,nextUserMapper));
Callable<List> qfe =new test(start,num,nextUserMapper);
//System.out.println(Thread.currentThread().getName());
futureList.add(qfe);
start += num;
}
List<Future<List>> futures=executorService.invokeAll(futureList);
//处理线程返回结果
if(futures!=null&&futures.size() > 0){
for (Future<List> future:futures){
result.addAll(future.get());
}
}
executorService.shutdown();//关闭线程池
// ForkJoinPool pool = new ForkJoinPool(10);
// test1 innerFind = new test1(0,count,nextUserMapper);
// result = pool.invoke(innerFind);
// nextUserMapper.getUsers(0,count);
long endTime = System.currentTimeMillis();
System.out.println(startTime - endTime);
}
}
主代码:
package com.next.jiangzh.film.dao;
import java.util.List;
import java.util.concurrent.Callable;
import com.next.jiangzh.film.dao.mapper.NextUserMapper;
public class test implements Callable<List> {
private int start;//当前页数
private int num;//每页查询多少条
private List page;//每次分页查出来的数据
public test(int start,int num,NextUserMapper nextUserMapper) {
this.start=start;
this.num=num;
//分页查询数据库数据
System.out.println(Thread.currentThread().getName());
page=nextUserMapper.getUsers(start, num);
}
@Override
public List call() throws Exception {
//返回数据给Future
return page;
}
}
控制台结果:
可以看到34785条数据查询了 871毫秒,但打印的线程显示都是主线程,所以事实上还是单线程查询。
为此这里使用方法二:forkJoin
主代码:
package com.next.jiangzh.film.dao;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.RecursiveTask;
import com.next.jiangzh.film.dao.mapper.NextUserMapper;
public class test1 extends RecursiveTask<List>{
private final static int COUNT = 5000;
private int startIndex;
private int endIndex;
private NextUserMapper nextUserMapper;
public test1( int startIndex,int endIndex,NextUserMapper nextUserMapper) {
this.startIndex = startIndex;
this.endIndex = endIndex;
this.nextUserMapper = nextUserMapper;
}
@Override
public List compute() {
if(endIndex - startIndex <= COUNT) {
System.out.println(Thread.currentThread().getName());
List page=nextUserMapper.getUsers(startIndex, endIndex - startIndex);
return page;
}else {//这里是递归直到每次查询的数据小于COUNT为止
int mid = (startIndex+endIndex)/2;
test1 left = new test1(startIndex,mid,nextUserMapper);
test1 right = new test1(mid,endIndex,nextUserMapper);
invokeAll(left,right);
left.getRawResult().addAll(right.getRawResult());
return left.getRawResult();
}
}
}
测试类:
package com.next.jiangzh.film.dao.mapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.next.jiangzh.film.dao.test;
import com.next.jiangzh.film.dao.test1;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
@RunWith(SpringRunner.class)
@SpringBootTest
public class FilmFieldTMapperTest2 {
@Autowired
private NextUserMapper nextUserMapper;
@Test
public void describeFieldListTest() throws InterruptedException, ExecutionException{
List<List> result = new ArrayList<>();//返回结果
long startTime = System.currentTimeMillis();
//查询数据库总数量
int count = nextUserMapper.count();
System.out.println(count);
ForkJoinPool pool = new ForkJoinPool(10);
test1 innerFind = new test1(0,count,nextUserMapper);
result = pool.invoke(innerFind);
//nextUserMapper.getUsers(0,count);
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
}
}
可以看到控制台结果34785条数据开启了
ForkJoinPool-1-worker-19
ForkJoinPool-1-worker-5
ForkJoinPool-1-worker-9
ForkJoinPool-1-worker-23
ForkJoinPool-1-worker-13
ForkJoinPool-1-worker-31
ForkJoinPool-1-worker-27
ForkJoinPool-1-worker-17
线程执行,耗时670毫秒,因为数据较少所以不太明显,但明显是快了些的
最后再看一下普通的一次性查询耗时745毫秒,当然这样如果数据量过大肯定会造成查询效率极低,还是数据太少不够直观,但明显forkJoin线程池的执行效率高点