main方法测试,定义for循环造假数据,定义time在方法结束后时间相减获取执行毫秒数,单线程跟多线程对比执行时间。
上图
我定义了2000000的数据量,可以看到单线程执行时间为6520毫秒,多线程(3个)执行时间为3000毫秒左右,执行速度快了一半,所以在大数据量的查询和插入时可以根据需求使用多线程。(注意线程同步和线程锁的使用:synchronized,否则可能会出现多线程对同一数据进行重复操作,违背数据原子性的情况)
上代码,直接拷贝两个类的代码即可执行测试多线程的使用
getData(主执行类)
package com.springboot.demo.multithreading;import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class getData {public static int total = 2000000;// 定义循环次数public static void main(String[] args) {System.err.println("下面为单线程执行时间------------------------");long time = new Date().getTime();// 获得执行请求的开始time值getData d = new getData();// 获取数据List<Map<String,Object>> list = d.getData();List<Map<String,Object>> list2 = d.getData2();List<Map<String,Object>> list3 = d.getData3();// 输出数量和执行时间System.err.println("1:"+list.size()+" 2:"+list2.size()+" 3:"+list3.size());System.err.println("耗时"+(new Date().getTime()-time)+"毫秒");System.err.println("下面为多线程执行时间------------------------");// 多线程执行方法getDataThread.get();}public List<Map<String,Object>> getData(){List<Map<String,Object>> list = new ArrayList<>();for (int i = 0; i < total; i++) {Map<String,Object> map = new HashMap<>();map.put("date", new Date().getTime());map.put("i", 1);list.add(map);}return list;}public List<Map<String,Object>> getData2(){List<Map<String,Object>> list = new ArrayList<>();for (int i = 0; i < total; i++) {Map<String,Object> map = new HashMap<>();map.put("date", new Date().getTime());map.put("i", 2);list.add(map);}return list;}public List<Map<String,Object>> getData3(){List<Map<String,Object>> list = new ArrayList<>();for (int i = 0; i < total; i++) {Map<String,Object> map = new HashMap<>();map.put("date", new Date().getTime());map.put("i", 3);list.add(map);}return list;}
}
getDataThread (多线程类)
package com.springboot.demo.multithreading;import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class getDataThread implements Runnable{private static long time;@Overridepublic void run() {// 根据传递参数多线程查询不同数据if (Thread.currentThread().getName().equals("1")) {System.err.println("线程1:数据量"+getData().size()+" 耗时:"+(new Date().getTime()-time));}if (Thread.currentThread().getName().equals("2")) {System.err.println("线程2:数据量"+getData2().size()+" 耗时:"+(new Date().getTime()-time));}if (Thread.currentThread().getName().equals("3")) {System.err.println("线程3:数据量"+getData3().size()+" 耗时:"+(new Date().getTime()-time));}}public static void get() {time = new Date().getTime();// 定义方法开始时间getDataThread thread = new getDataThread(); // 定义3个线程执行Thread t1 = new Thread(thread, "1");Thread t2 = new Thread(thread, "2");Thread t3 = new Thread(thread, "3");t1.start();t2.start();t3.start();}public List<Map<String,Object>> getData(){List<Map<String,Object>> list = new ArrayList<>();for (int i = 0; i < getData.total; i++) {Map<String,Object> map = new HashMap<>();map.put("date", new Date().getTime());map.put("i", 1);list.add(map);}return list;}public List<Map<String,Object>> getData2(){List<Map<String,Object>> list = new ArrayList<>();for (int i = 0; i < getData.total; i++) {Map<String,Object> map = new HashMap<>();map.put("date", new Date().getTime());map.put("i", 2);list.add(map);}return list;}public List<Map<String,Object>> getData3(){List<Map<String,Object>> list = new ArrayList<>();for (int i = 0; i < getData.total; i++) {Map<String,Object> map = new HashMap<>();map.put("date", new Date().getTime());map.put("i", 3);list.add(map);}return list;}
}