package com.mybatislomboktest.generatortest;
import com.mybatislomboktest.generatortest.dao.StuMapper;
import com.mybatislomboktest.generatortest.dao.SysMapper;
import com.mybatislomboktest.generatortest.entity.Stu;
import com.mybatislomboktest.generatortest.entity.Sys;
import com.mybatislomboktest.generatortest.util.TestBatchInsertThread;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import tk.mybatis.mapper.entity.Example;
import java.util.*;
import java.util.concurrent.*;
@SpringBootTest
class GeneratorTestApplicationTests {
@Autowired
private SysMapper sysMapper;
@Autowired
private StuMapper stuMapper;
@Test
void contextLoads() {
for (int i = 0; i < 10000; i++) {
// Sys sys = new Sys();
// sys.setSysName("张三" + i);
// sys.setSysPwd(123456);
// sys.setSysSex(i%2 == 0 ? "男" : "女");
// sysMapper.insert(sys);
Stu stu = new Stu();
stu.setGrade("班级" + i);
stu.setSysId(i + 1);
stuMapper.insert(stu);
}
}
@Test
void Test3() throws InterruptedException {
// int commitSize = 500;
//
// ExecutorService executor = Executors.newFixedThreadPool(10);
// int count = syss.size() % commitSize;
// int size = syss.size();
// System.out.println(size);
// System.out.println(count);
// List<Stu> stuList = new ArrayList<>();
// for (int i = 0; i < syss.size(); i++) {
// Stu stu = new Stu();
// stu.setGrade("班级" + syss.get(i).getId());
// stu.setSysId(syss.get(i).getId());
// stuList.add(stu);
// if (stuList.size() == commitSize){
// System.out.println(stuList.size());
// executor.submit(() -> {
// for (Stu stu1 : stuList) {
// stuMapper.insert(stu1);
// try {
//
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// });
// stuList.clear();
// }
// if (i > size - count){
// for (Stu stu1 : stuList) {
// stuMapper.insert(stu1);
// }
// }
// }
// executor.shutdown();
// while(true){
// if(executor.isTerminated()){
// System.out.println("插入完成!");
// break;
// }
// Thread.sleep(1000);
// }
//
}
@Test
void batchInsertByThread() throws Exception {
List<Sys> list = sysMapper.selectAll();
if (list == null || list.isEmpty()) {
return;
}
// 一个线程处理1000条数据
int count = 1000;
// 数据集合大小
int listSize = list.size();
// 开启的线程数
int runSize = (listSize / count) + 1;
// 存放每个线程的执行数据
List<Stu> newList = new ArrayList<>();
List<Sys> new1List = null;
// 创建一个线程池,数量和开启线程的数量一样
ExecutorService executor = Executors.newFixedThreadPool(runSize);
// 创建两个个计数器
CountDownLatch begin = new CountDownLatch(1);
CountDownLatch end = new CountDownLatch(runSize);
for (int i = 0; i < runSize; i++) {
/* 计算每个线程执行的数据 */
if ((i + 1) == runSize) {
int startIdx = (i * count);
int endIdx = list.size();
new1List = list.subList(startIdx, endIdx);
newList = new CopyOnWriteArrayList<>();
for (int j = 0; j < new1List.size(); j++) {
Stu stu = new Stu();
stu.setGrade("Grade" + new1List.get(j).getId());
stu.setSysId(new1List.get(j).getId());
newList.add(stu);
}
} else {
int startIdx = (i * count);
int endIdx = (i + 1) * count;
new1List = list.subList(startIdx, endIdx);
newList = new ArrayList<>();
for (int j = 0; j < new1List.size(); j++) {
Stu stu = new Stu();
stu.setGrade("Grade" + new1List.get(j).getId());
stu.setSysId(new1List.get(j).getId());
newList.add(stu);
}
}
TestBatchInsertThread testBatchInsertThread = new TestBatchInsertThread(newList, begin, end, stuMapper);
executor.execute(testBatchInsertThread);
}
System.out.println(Thread.currentThread().getName() + "=================》开始执行插入操作");
begin.countDown();
end.await();
System.out.println(Thread.currentThread().getName() + "=================》插入操作完成");
executor.shutdown();
}
@Test
void Test2() throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(10);
List<Sys> resultList = Collections.synchronizedList(new ArrayList<>());
for (int i = 1; i <= 10000 / 500; i++) {
final int i1 = i;
Future<?> future = executorService.submit(new Callable<Object>() {
@Override
public List<Sys> call() {
Example example = new Example(Sys.class);
example.createCriteria().andGreaterThan("id", 500 * (i1 - 1)).andLessThanOrEqualTo("id", 500 * i1);
List<Sys> list = sysMapper.selectByExample(example);
return list;
}
});
try {
System.out.println(future.get());
resultList.addAll((Collection<? extends Sys>) future.get());
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
executorService.shutdown();
while (true) {
if (executorService.isTerminated()) {
System.out.println("所有的子线程都结束了!");
break;
}
Thread.sleep(1000);
}
System.out.println(resultList.size());
}
}