1、配置扫描的类,线程使用的ApplicationContextUtils需要被扫描到

<context:component-scan base-package="com.glanway.zpparts.controller,com.glanway.zpparts.test"/>

2、编写一个ApplicationContextUtils对象的类,与上面设置的路径对应

 package com.glanway.zpparts.test;import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;@Component
public class ApplicationContextUtils implements ApplicationContextAware {@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {SpringUtils.setApplicationContext(applicationContext);}}

3、编写一个获取Sservice的SpringUtils工具类,路径与上面配置的对应

 package com.glanway.zpparts.test;import org.springframework.context.ApplicationContext;/*** 注入获取service对象* @author zp_ww* @date 2019年3月25日*/
public class SpringUtils {private static ApplicationContext applicationContext;public static void setApplicationContext(ApplicationContext applicationContext) {SpringUtils.applicationContext = applicationContext;}public static ApplicationContext getApplicationContext() {return SpringUtils.applicationContext;}}

4、编写一个创建线程池的类

 package com.glanway.zpparts.test;import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;/*** 创建线程池* @author zp_ww* @date 2019年3月25日*/
public class ThreadPoolHelper {private static ThreadPoolExecutor threadPoolExecutor;static {// 设置线程池同时进行的线程数、创建的线程数、失效时间threadPoolExecutor = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());}public static void execute(Runnable runnable) {threadPoolExecutor.execute(runnable);}}

5、创建一个正确数据跑的线程方法

 package com.glanway.zpparts.test;import java.util.List;import org.apache.poi.ss.usermodel.Row;
import org.springframework.context.ApplicationContext;import com.glanway.zpparts.service.wyyx.WyYxUserService;/*** 正确线程实现类* @author zp_ww* @date 2019年3月25日*/
public class ExcelTask implements Runnable {//开始索引private int startIndex;//处理的条数private int total;//sheetprivate List<Row> row;public ExcelTask(List<Row> row, int startIndex, int total) {this.startIndex = startIndex;this.row = row;this.total = total;}/*** 线程的实现方法*/@Overridepublic void run() {//WyYxUserService wyyxUserService = SpringUtils.getBean(WyYxUserService.class);//WyyxUser user = wyyxUserService.getWyYxUserByMemberId(Long.valueOf(16));//ZppartsServiceUtilTwo.getService();ApplicationContext applicationContext = SpringUtils.getApplicationContext();// 获取serviceWyYxUserService wyYxUserService = applicationContext.getBean(WyYxUserService.class);System.out.println(wyYxUserService);System.out.println(wyYxUserService.getWyYxUserByMemberId(Long.valueOf(16)));//        WyyxUser user = ZppartsServiceUtils.getZppartsServiceUtils().getWyYxUserService().getWyYxUserByMemberId(Long.valueOf(16));
//        System.out.println(user.getName());
//        System.out.println(Thread.currentThread().getName() + " 处理成功数据:"+startIndex + ":" + (startIndex + total));
//        for (int i = startIndex; i < total; i++) {
//
//        }}public int getStartIndex() {return startIndex;}public void setStartIndex(int startIndex) {this.startIndex = startIndex;}public int getTotal() {return total;}public void setTotal(int total) {this.total = total;}public List<Row> getRow() {return row;}public void setRow(List<Row> row) {this.row = row;}}

6、创建一个错误数据跑的线程实现类

 package com.glanway.zpparts.test;import java.util.List;import org.apache.poi.ss.usermodel.Row;/*** 错误数据线程实现类* @author zp_ww* @date 2019年3月25日*/
public class ExcelErrorTask implements Runnable {//sheetprivate List<Row> row;public ExcelErrorTask(List<Row> row) {this.row = row;}@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + " 处理失败数据:" + row.size());}public List<Row> getRow() {return row;}public void setRow(List<Row> row) {this.row = row;}}

7、编写一个测试类

package com.glanway.zpparts.test;import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;/*** 线程测试* @author zp_ww* @date 2019年3月25日*/
public class ExcelUpload {public static List<Row> resultList = new ArrayList<>();public static List<Row> errorList = new ArrayList<>();public static void test() throws Exception {long startTime = System.currentTimeMillis();Set<String> heavySet = new HashSet<String>();int lastSize = 0;File file =new File("C:/Users/zp_ww/Documents/WeChat Files/Files/数据/数据", "商品导入测试.xlsx");FileInputStream inputStream = new FileInputStream(file);XSSFWorkbook workbook = new XSSFWorkbook(inputStream);System.out.println("读取文件耗时: " + (System.currentTimeMillis() - startTime));Sheet sheet = workbook.getSheetAt(0);int rowNumber = sheet.getLastRowNum(); // 第一行从0开始算for (int i = 1; i <= rowNumber; i++) {Row row = sheet.getRow(i);row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);String stringCellValue = row.getCell(4).getStringCellValue().replace(" ", "");String stringCellValue2 = row.getCell(7).getStringCellValue().replace(" ", "");heavySet.add(stringCellValue + stringCellValue2);if (heavySet.size() == lastSize) {// 插入失敗errorList.add(row);} else {// 插入成功resultList.add(row);lastSize = heavySet.size();}}System.out.println("文件去重耗时: " + (System.currentTimeMillis() - startTime));int resultListSize = resultList.size();// 每个线程最低执行的行数int count = (int)(resultListSize / 5);// 多余的行数int num = resultListSize - (count * 5);for (int i = 0; i < 5; i++) {if (i == 4) {// 创建一个线程ThreadPoolHelper.execute(new ExcelTask(resultList, count * i, count + num));} else {ThreadPoolHelper.execute(new ExcelTask(resultList, count * i, count));}}ThreadPoolHelper.execute(new ExcelErrorTask(errorList));}}

一个简单的多线程实现相关推荐

  1. Java Tread多线程(0)一个简单的多线程实例

    作者 : 卿笃军 原文地址:http://blog.csdn.net/qingdujun/article/details/39341887 本文演示,一个简单的多线程实例,并简单分析一下线程. 编程多 ...

  2. VC菜菜鸟-创建一个简单的多线程任务

    在学习编程的过程中,经典的"HelloWorld"给我最大的启示就是,一切从简,简而明理. 写一个连"白痴"都看得懂的代码,是编程的最高境界. 想要学一个知识点 ...

  3. Qt5.9一个简单的多线程实例(类QThread)(第一种方法)

    Qt开启多线程,主要用到类QThread.有两种方法,第一种用一个类继承QThread,然后重新改写虚函数run().当要开启新线程时,只需要实例该类,然后调用函数start(),就可以开启一条多线程 ...

  4. 多线程爬虫python_一个简单的多线程Python爬虫

    最近想要抓取拉勾网的数据,最开始是使用Scrapy的,但是遇到了下面两个问题: 前端页面是用JS模板引擎生成的 接口主要是用POST提交参数的 目前不会处理使用JS模板引擎生成的HTML页面,用POS ...

  5. python写tcp服务器_用Python实现一个简单的多线程TCP服务器的教程

    最近看<python核心编程>,书中实现了一个简单的1对1的TCPserver,但是在实际使用中1对1的形势明显是不行的,所以研究了一下如何在server端通过启动不同的线程(进程)来实现 ...

  6. 【Java线程安全】一个简单的多线程程序

    安全示例1 同步块 package cn.hanquan.test;public class HappyTrain {public static void main(String[] args) th ...

  7. 编写一个简单的linux kernel rootkit

    一.前言 linux kernel rootkit跟普通的应用层rootkit个人感觉不大,个人感觉区别在于一个运行在用户空间中,一个运行在内核空间中:另一个则是编写时调用的API跟应用层rootki ...

  8. java timetasker_Java网络与多线程系列之1:实现一个简单的对象池

    前言 为什么要从对象池开始呢,先从一个网络IO操作的demo说起 比如下面这段代码,显而易见已经在代码中使用了一个固定大小的线程池,所以现在的重点在实现Runnble接口的匿名对象上,这个对象每次创建 ...

  9. 使用qt多线程的一个简单方法

    有时候在gui编程中需要调用一个非常耗费时间的api类的函数,这个时候不使用多线程的话界面就会卡死.使用多线程有一个非常简单的办法,不需要建立新的QThread派生类. 设需要调用的api定义为 bo ...

  10. 通信软件基础B-重庆邮电大学-Java-编程实现一个简单的聊天程序-多线程编程实现

    实验任务六 编程实现一个简单的聊天程序-多线程编程实现 1. 系统设计要求 编程实现一个简单的聊天程序,实现两台计算机间的信息交互,使用多线程编程实现:可同时连接多个客户端,服务器收到客户端发送的消息 ...

最新文章

  1. VCenter (虚拟架构环境的集中管理) 、数据库虚拟机迁移的影响
  2. 轻轻松松明白什么是反射,反射有什么用,简单上手反射以及反射的优缺点
  3. c语言程序设计俄罗斯方块PPT,俄罗斯方块游戏:C语言程序设计初步感受
  4. linux内核修改工程环境,linux2.6.33内核移植s3c2410以和交叉环境编译搭建最终修改版.pdf...
  5. JBoss 4.2.x Spring 3 JPA Hibernate教程
  6. 软件测试度量计算方法有哪些,软件测试度量(三)
  7. 推文科技:AI解决方案助力内容出海
  8. java-web-j2e学习建议路线
  9. 为什么说ES6的class是语法糖?
  10. MYSQL数据库导入大数据量sql文件失败的解决方案
  11. nxp EIQ无法使用脚本导入数据集:ssl.SSLCertVerificationError
  12. 某些型号的Comba和D-Link路由器存在管理员密码泄露漏洞
  13. k8s serviceAccountName填写后应用没有进行挂载问题处理
  14. c 语言编程字谜,字谜游戏(a)C语言
  15. 数组的并集交集和差集
  16. Git通过SSH拉取报错kex_exchange_identification
  17. java统计 pv uv_统计网址的pv,uv(附带Spring定时器)
  18. 公式不懂也无妨,业务精通才是真正的算法工程师
  19. Centos Ubuntu 安装 gfortran
  20. android 发送短信例子

热门文章

  1. arcengine 加载地图不显示_Devexpress使用后arcengine地图加载不能全图显示
  2. 怎样用计算机控制插座,电脑顺序开、关机控制插座 二
  3. 527. Word Abbreviation
  4. android 自定义textview在onlayout中设置setTypeface的时候报错 requestLayout() improperly called by ...
  5. Java多线程篇--线程的等待通知
  6. CentOS7 修改Swap大小
  7. STM32仿真器下载配置
  8. ZYNQ仿真器当做串口使用
  9. 详解 Flutter engine多线程、Dart isolate和异步
  10. 【测评】国外AR平台ENTITI测评-网页编辑器(1)