首页 > 代码库 > 单线程和多线程处理1W条数据对比代码
单线程和多线程处理1W条数据对比代码
package study.interview; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.Random; import java.util.Set; public class TestEmailFromList { final static String[] list = {"qq.com","126.com","168.com","sina.com","baidu.com","soho.com","yeah.net","139.com","hotmail.com"}; public static void main(String[] args) throws InterruptedException { //email存放容器 LinkedList<Email> emailList = new LinkedList<Email>(); //统计各类邮箱使用人数容器 Map<String, Integer> mapEmail = new HashMap<String, Integer>(); //统计各类邮箱使用人数容器 Map<String, Integer> mapEmailPool = new HashMap<String, Integer>(); Random random=new Random(); //email原始邮箱初始化 for (int i = 0; i < 9999; i++) { int num = random.nextInt(9); emailList.add(new Email(String.valueOf(i+"@"+list[num]))); } // 单线程统计各类邮箱使用人数 long startTime = System.currentTimeMillis(); TestEmailFromList.countingBySingleThread(emailList, mapEmail); long endTime = System.currentTimeMillis(); System.out.println("单线程统计用时:"+(endTime-startTime)); //多线程统计各类邮箱使用人数 long startTime2 = System.currentTimeMillis(); //用多少个线程 TestEmailFromList.countingByMultiThread(emailList, mapEmailPool); long endTime2 = System.currentTimeMillis(); System.out.println("多线程统计用时:"+(endTime2-startTime2)); } /* * 单线程统计邮箱使用人数 */ public static void countingBySingleThread(LinkedList<Email> emailList,Map<String, Integer> mapEmail){ for (int i = 0; i <emailList.size(); i++) { String key = emailList.get(i).getUserName().split("@")[1]; if(mapEmail.containsKey(key)){ int value = http://www.mamicode.com/mapEmail.get(key);"----"+eList.getLast().getUserName()); //使用ExecutorService执行Callable类型的任务,并将结果保存在future变量中 Future<Map<String, Integer>> future = executorService.submit(new TaskWithResultMap( eList,mapEmailPool)); //将任务执行结果存储到List中 resultList.add(future); } //遍历任务的结果 for (Future<Map<String, Integer>> fs : resultList) { try { System.out.println(fs.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); }finally{ executorService.shutdown(); } } printMap(mapEmailPool); } /* * 输出map */ public static void printMap(Map<String, Integer> mapEmail){ Set<Entry<String, Integer>> set = mapEmail.entrySet(); for (Entry<String, Integer> entry : set) { System.out.println("使用"+entry.getKey()+"的人共"+entry.getValue()); } } } class TaskWithResultMap implements Callable<Map<String, Integer>>{ LinkedList<Email> emailList; Map<String, Integer> mapEmailPool; public TaskWithResultMap(LinkedList<Email> emailList,Map<String, Integer> mapEmailPool){ this.emailList = emailList; this.mapEmailPool = mapEmailPool; } @Override public Map<String, Integer> call() throws Exception { synchronized (mapEmailPool) { for (int i = 0; i <emailList.size(); i++) { String key = emailList.get(i).getUserName().split("@")[1]; if(mapEmailPool.containsKey(key)){ int value = http://www.mamicode.com/mapEmailPool.get(key);"@")[1]; if(mapEmailPool.containsKey(key)){ int value = http://www.mamicode.com/mapEmailPool.get(key);"异常"); } } } } } class Email { String username; public Email() { } public Email(String username) { this.username = username; } public String getUserName() { return username; } }
单线程和多线程处理1W条数据对比代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。