摘要:背景对于多线程的理解不是非常深刻,工作中用到多线程代码的机会也不多,前不久遇到了一个使用场景,通过编码实现后对于多线程的理解和应用有了更加深刻的理解。多线程发送短信中的一个核心要点是,将全部手机号码拆分成多个组后,分配给每个线程进行执行。
背景
对于多线程的理解不是非常深刻,工作中用到多线程代码的机会也不多,前不久遇到了一个使用场景,通过编码实现后对于多线程的理解和应用有了更加深刻的理解。场景如下:现有给用户发送产品调研的需求,运营的同事拿来了一个Excel文件,要求给Excel里面大约六万个手机号发送调研短信。
最简单的方法就是一个循环然后单线程顺序发送,但是核心问题在于,给短信运营商发短信的接口响应时间较长,假设平均100ms的响应时间,那么单线程发送的话需要6万*0.1秒=6000秒。显然这个时间是不能接受的,运营商系统的发送接口我们是不能优化的,只得增强自己的发送和处理能力才能尽快的完成任务。
批量发短信 读取Excel中的信息 包依赖工具类代码,Maven中引入如下两个包
读取Excel的工具类代码org.apache.poi poi-ooxml 3.17 org.apache.xmlbeans xmlbeans 2.6.0
/** * 读取Excel的文件信息 * * @param fileName */ public static void readFromExcel(String fileName) { InputStream is = null; try { is = new FileInputStream(fileName); XSSFWorkbook workbook = new XSSFWorkbook(is); XSSFSheet sheet = workbook.getSheetAt(0); int num = 0; // 循环行Row for (int rowNum = 0, lastNum = sheet.getLastRowNum(); rowNum <= lastNum; rowNum++) { XSSFRow row = sheet.getRow(rowNum); String phoneNumber = getStringValueFromCell(row.getCell(0)).trim(); phoneList.add(phoneNumber); } System.out.println(num); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 读取Excel里面Cell内容 * * @param cell * @return */ private static String getStringValueFromCell(XSSFCell cell) { // 单元格内的时间格式 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // 单元格内的数字类型 DecimalFormat decimalFormat = new DecimalFormat("#.#####"); // 单元格默认为空 String cellValue = ""; if (cell == null) { return cellValue; } // 按类型读取 if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) { cellValue = cell.getStringCellValue(); } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { // 日期转为时间形式 if (DateUtil.isCellDateFormatted(cell)) { double d = cell.getNumericCellValue(); Date date = DateUtil.getJavaDate(d); cellValue = dateFormat.format(date); } else { // 其他转为数字 cellValue = decimalFormat.format((cell.getNumericCellValue())); } } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BLANK) { cellValue = ""; } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) { cellValue = String.valueOf(cell.getBooleanCellValue()); } else if (cell.getCellType() == XSSFCell.CELL_TYPE_ERROR) { cellValue = ""; } else if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) { cellValue = cell.getCellFormula().toString(); } return cellValue; }模拟运营商发送短信的方法
/** * 外部接口耗时长,通过多线程增强 * * @param userPhone */ public void sendMsgToPhone(String userPhone) { try { Thread.sleep(SEND_COST_TIME); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("send message to : " + userPhone); }多线程发短信 简单的单线程发送
/** * 单线程发送 * * @param phoneList * @return */ private long singleThread(ListphoneList) { long start = System.currentTimeMillis(); /*// 直接主线程执行 for (String phoneNumber : phoneList) { threadOperation.sendMsgToPhone(phoneNumber); }*/ SendMsgExtendThread smet = threadOperation.new SendMsgExtendThread(phoneList); smet.start(); long totalTime = System.currentTimeMillis() - start; System.out.println("单线程发送总时间:" + totalTime); return totalTime; }
对于大批量发短信的场景,如果使用单线程将全部一千个号码发送完毕的话,大约需要103132ms,可见效率低下,耗费时间较长。
多线程发送短信中的一个核心要点是,将全部手机号码拆分成多个组后,分配给每个线程进行执行。
两个线程的示例/** * 两个线程发送 * * @param phoneList * @return */ private long twoThreads(List另一种数据分组方式phoneList) { long start = System.currentTimeMillis(); List list1 = phoneList.subList(0, phoneList.size() / 2); List list2 = phoneList.subList(phoneList.size() / 2, phoneList.size()); SendMsgExtendThread smet = threadOperation.new SendMsgExtendThread(list1); smet.start(); SendMsgExtendThread smet1 = threadOperation.new SendMsgExtendThread(list2); smet1.start(); return 0; }
/** * 另外一种分配方式 * * @param phoneList */ private void otherThread(List线程池发送phoneList) { for (int threadNo = 0; threadNo < 10; threadNo++) { int numbersPerThread = 10; List list = phoneList.subList(threadNo * numbersPerThread, (threadNo * numbersPerThread) + 10); SendMsgExtendThread smet = threadOperation.new SendMsgExtendThread(list); smet.start(); if (list.size() < numbersPerThread) { break; } } }
/** * 线程池发送 * * @param phoneList * @return */ private void threadPool(List使用Callable发送phoneList) { for (int threadNo = 0; threadNo < THREAD_POOL_SIZE; threadNo++) { int numbersPerThread = 10; List list = phoneList.subList(threadNo * numbersPerThread, (threadNo * numbersPerThread) + 10); threadOperation.executorService.execute(threadOperation.new SendMsgExtendThread(list)); } threadOperation.executorService.shutdown(); }
/** * 多线程发送 * * @param phoneList * @return */ private void multiThreadSend(ListphoneList) { List > futures = new ArrayList<>(); for (int threadNo = 0; threadNo < THREAD_POOL_SIZE; threadNo++) { int numbersPerThread = 100; List list = phoneList.subList(threadNo * numbersPerThread, (threadNo * numbersPerThread) + 100); Future future = threadOperation.executorService.submit(threadOperation.new SendMsgImplCallable(list, String.valueOf(threadNo))); futures.add(future); } for (Future future : futures) { try { System.out.println(future.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } threadOperation.executorService.shutdown(); }
使用多线程发送,将发送任务进行分割然后分配给每个线程执行,执行完毕需要10266ms,可见执行效率明显提升,消耗时间明显缩短。
完整代码package com.lingyejun.tick.authenticator; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.*; public class ThreadOperation { // 发短信的同步等待时间 private static final long SEND_COST_TIME = 100L; // 手机号文件 private static final String FILE_NAME = "/Users/lingye/Downloads/phone_number.xlsx"; // 手机号列表 private static ListphoneList = new ArrayList<>(); // 单例对象 private static volatile ThreadOperation threadOperation; // 线程个数 private static final int THREAD_POOL_SIZE = 10; // 初始化线程池 private ExecutorService executorService = new ThreadPoolExecutor(THREAD_POOL_SIZE, THREAD_POOL_SIZE, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue ()); public ThreadOperation() { // 从本地文件中读取手机号码 readFromExcel(FILE_NAME); } public static void main(String[] args) { ThreadOperation threadOperation = getInstance(); //threadOperation.singleThread(phoneList); threadOperation.multiThreadSend(phoneList); } /** * 单例获取对象 * * @return */ public static ThreadOperation getInstance() { if (threadOperation == null) { synchronized (ThreadOperation.class) { if (threadOperation == null) { threadOperation = new ThreadOperation(); } } } return threadOperation; } /** * 读取Excel的文件信息 * * @param fileName */ public static void readFromExcel(String fileName) { InputStream is = null; try { is = new FileInputStream(fileName); XSSFWorkbook workbook = new XSSFWorkbook(is); XSSFSheet sheet = workbook.getSheetAt(0); int num = 0; // 循环行Row for (int rowNum = 0, lastNum = sheet.getLastRowNum(); rowNum <= lastNum; rowNum++) { XSSFRow row = sheet.getRow(rowNum); String phoneNumber = getStringValueFromCell(row.getCell(0)).trim(); phoneList.add(phoneNumber); } System.out.println(num); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 读取Excel里面Cell内容 * * @param cell * @return */ private static String getStringValueFromCell(XSSFCell cell) { // 单元格内的时间格式 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // 单元格内的数字类型 DecimalFormat decimalFormat = new DecimalFormat("#.#####"); // 单元格默认为空 String cellValue = ""; if (cell == null) { return cellValue; } // 按类型读取 if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) { cellValue = cell.getStringCellValue(); } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { // 日期转为时间形式 if (DateUtil.isCellDateFormatted(cell)) { double d = cell.getNumericCellValue(); Date date = DateUtil.getJavaDate(d); cellValue = dateFormat.format(date); } else { // 其他转为数字 cellValue = decimalFormat.format((cell.getNumericCellValue())); } } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BLANK) { cellValue = ""; } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) { cellValue = String.valueOf(cell.getBooleanCellValue()); } else if (cell.getCellType() == XSSFCell.CELL_TYPE_ERROR) { cellValue = ""; } else if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) { cellValue = cell.getCellFormula().toString(); } return cellValue; } /** * 外部接口耗时长,通过多线程增强 * * @param userPhone */ public void sendMsgToPhone(String userPhone) { try { Thread.sleep(SEND_COST_TIME); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("send message to : " + userPhone); } /** * 单线程发送 * * @param phoneList * @return */ private long singleThread(List phoneList) { long start = System.currentTimeMillis(); /*// 直接主线程执行 for (String phoneNumber : phoneList) { threadOperation.sendMsgToPhone(phoneNumber); }*/ SendMsgExtendThread smet = threadOperation.new SendMsgExtendThread(phoneList); smet.start(); long totalTime = System.currentTimeMillis() - start; System.out.println("单线程发送总时间:" + totalTime); return totalTime; } /** * 另外一种分配方式 * * @param phoneList */ private void otherThread(List phoneList) { for (int threadNo = 0; threadNo < 10; threadNo++) { int numbersPerThread = 10; List list = phoneList.subList(threadNo * numbersPerThread, (threadNo * numbersPerThread) + 10); SendMsgExtendThread smet = threadOperation.new SendMsgExtendThread(list); smet.start(); if (list.size() < numbersPerThread) { break; } } } /** * 两个线程发送 * * @param phoneList * @return */ private long twoThreads(List phoneList) { long start = System.currentTimeMillis(); List list1 = phoneList.subList(0, phoneList.size() / 2); List list2 = phoneList.subList(phoneList.size() / 2, phoneList.size()); SendMsgExtendThread smet = threadOperation.new SendMsgExtendThread(list1); smet.start(); SendMsgExtendThread smet1 = threadOperation.new SendMsgExtendThread(list2); smet1.start(); return 0; } /** * 线程池发送 * * @param phoneList * @return */ private void threadPool(List phoneList) { for (int threadNo = 0; threadNo < THREAD_POOL_SIZE; threadNo++) { int numbersPerThread = 10; List list = phoneList.subList(threadNo * numbersPerThread, (threadNo * numbersPerThread) + 10); threadOperation.executorService.execute(threadOperation.new SendMsgExtendThread(list)); } threadOperation.executorService.shutdown(); } /** * 多线程发送 * * @param phoneList * @return */ private void multiThreadSend(List phoneList) { List > futures = new ArrayList<>(); for (int threadNo = 0; threadNo < THREAD_POOL_SIZE; threadNo++) { int numbersPerThread = 100; List list = phoneList.subList(threadNo * numbersPerThread, (threadNo * numbersPerThread) + 100); Future future = threadOperation.executorService.submit(threadOperation.new SendMsgImplCallable(list, String.valueOf(threadNo))); futures.add(future); } for (Future future : futures) { try { System.out.println(future.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } threadOperation.executorService.shutdown(); } public class SendMsgExtendThread extends Thread { private List numberListByThread; public SendMsgExtendThread(List numberList) { numberListByThread = numberList; } @Override public void run() { long startTime = System.currentTimeMillis(); for (int i = 0; i < numberListByThread.size(); i++) { System.out.print("no." + (i + 1)); sendMsgToPhone(numberListByThread.get(i)); } System.out.println("== single thread send " + numberListByThread.size() + "execute time:" + (System.currentTimeMillis() - startTime) + " ms"); } } public class SendMsgImplCallable implements Callable { private List numberListByThread; private String threadName; public SendMsgImplCallable(List numberList, String threadName) { numberListByThread = numberList; this.threadName = threadName; } @Override public Long call() throws Exception { Long startMills = System.currentTimeMillis(); for (String number : numberListByThread) { sendMsgToPhone(number); } Long endMills = System.currentTimeMillis(); return endMills - startMills; } } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/77405.html
摘要:从多线程的发展来看,可以操作系统的发展分为三个历史阶段真空管和穿孔卡片晶体管和批处理系统集成电路和多道程序设计最早的计算机只能解决简单的数学运算问题,比如正弦余弦等。我们用了比较长的篇幅介绍了进程线程发展的历史。 专题简介 作为一个合格的Java程序员,必须要对并发编程有一个深层次的了解,在很多互联网企业都会重点考察这一块。可能很多工作3年以上的Java程序员对于这一领域几乎没有太多研...
摘要:探测端口开放原理就是向目标发送请求,看是否有回应。端口判定为不通。扫描批量目标扫描批量目标使用的并发队列功能,去执行的执行单个任务,在扫描前做了一些额外的工作把浏览器屏蔽的端口过滤掉了,收到的状态就是。 0×00 前言 前两天 freebuf上的的XSS到内网的公开课很受启发,从一个页面到局域网,威力着实增强不少 公开课上检测内网 IP 实现方式用的是 img 标签,加载网站的 fav...
摘要:探测端口开放原理就是向目标发送请求,看是否有回应。端口判定为不通。扫描批量目标扫描批量目标使用的并发队列功能,去执行的执行单个任务,在扫描前做了一些额外的工作把浏览器屏蔽的端口过滤掉了,收到的状态就是。 0×00 前言 前两天 freebuf上的的XSS到内网的公开课很受启发,从一个页面到局域网,威力着实增强不少 公开课上检测内网 IP 实现方式用的是 img 标签,加载网站的 fav...
阅读 945·2023-04-26 02:56
阅读 9229·2021-11-23 09:51
阅读 1834·2021-09-26 10:14
阅读 2943·2019-08-29 13:09
阅读 2120·2019-08-26 13:29
阅读 521·2019-08-26 12:02
阅读 3486·2019-08-26 10:42
阅读 2981·2019-08-23 18:18