java多线程使用callable实现多线程返回值示例

直接上代码

/**
	 * 药品映射excel导入
	 * 
	 * @param file
	 * @param title
	 * @return
	 */
	@SuppressWarnings("resource")
	public String medicineExcelUpLoad(MultipartFile file, final String[] title, final String orgId) {
		final SysUserDto sysUser = JwtUtil.getCurrentSysUser();
		Map<String, Object> map = new HashMap<>();
		InputStream inputStream = null;
		Sheet sht0 = null;
		try {
			sds.deleteMedicine(orgId);
			inputStream = file.getInputStream();
			Workbook wb0 = new XSSFWorkbook(inputStream);
			sht0 = wb0.getSheetAt(0);
		} catch (IOException e2) {
			e2.printStackTrace();
		}
		int totalCount = sht0.getLastRowNum();
		int failerCount = 0;
		final CountDownLatch totalCountLatch = new CountDownLatch(totalCount);// 总数
		try {
			for (final Row r : sht0) {
				if (r.getRowNum() < 1) {
					continue;
				}
				Future<Integer> future = es.submit(new MedicineThread(sysUser, totalCountLatch, orgId, r, title));
				Integer errCount = future.get();
				failerCount += errCount;
			}
			totalCountLatch.await();
			long successCount = totalCount - failerCount;
			map.put("successCount", successCount);
			map.put("failerCount", failerCount);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				logger.error(e.getMessage());
				e.printStackTrace();
			}
		}
		return JSONObject.toJSONString(map);
	}

	class MedicineThread implements Callable<Integer> {
		private SysUserDto sysUser;
		private CountDownLatch totalCountLatch;
		private String orgId;
		private Row r;
		private String[] title;

		public MedicineThread(SysUserDto sysUser, CountDownLatch totalCountLatch, String orgId, Row r, String[] title) {
			this.sysUser = sysUser;
			this.totalCountLatch = totalCountLatch;
			this.orgId = orgId;
			this.r = r;
			this.title = title;
		}

		@SuppressWarnings({ "unchecked", "rawtypes" })
		@Override
		public Integer call() throws Exception {
			Integer errorCount = 0;
			Map mp = new HashMap<String, String>();
			for (int j = 0; j < title.length; j++) {
				Cell hc = r.getCell(j);
				if (hc != null) {
					hc.setCellType(CellType.STRING);
					mp.put(title[j], hc.getStringCellValue());
				}
			}

			MedMedicineMapping medMedicineMapping = null;
			MedMedicineMappingId medMedicineMappingId = null;
			try {
				medMedicineMapping = (MedMedicineMapping) MapUtil.convertMap(MedMedicineMapping.class, mp);
				medMedicineMappingId = (MedMedicineMappingId) MapUtil.convertMap(MedMedicineMappingId.class, mp);
				medMedicineMappingId.setOrgId(orgId);
				medMedicineMapping.setId(medMedicineMappingId);
				medMedicineMappingDao.save(medMedicineMapping);
				// 导入Excel,保存审核记录
				saveAudit(sysUser, orgId, "MED_MEDICINE_MAPPING", medMedicineMapping.getId().getCode(), "0");
			} catch (Exception e) {
				e.printStackTrace();
				try {
					MedMedicineMappingErr medMedicineMappingErr = (MedMedicineMappingErr) MapUtil.convertMap(MedMedicineMappingErr.class, mp);
					medMedicineMappingErr.setOrgId(medMedicineMapping.getId().getOrgId());
					medMedicineMappingErr.setCode(medMedicineMapping.getId().getCode());
					medMedicineMappingErr.setCollectNo(medMedicineMapping.getCollectNo());
					medMedicineMappingErr.setName(medMedicineMapping.getName());
					medMedicineMappingErr.setType(medMedicineMapping.getType());
					medMedicineMappingErr.setTypeCode(medMedicineMapping.getTypeCode());
					medMedicineMappingErr.setPharmType(medMedicineMapping.getPharmType());
					medMedicineMappingErr.setPharmTypeCode(medMedicineMapping.getPharmTypeCode());
					medMedicineMappingErr.setGeneralNameCode(medMedicineMapping.getGeneralNameCode());
					medMedicineMappingErr.setFormName(medMedicineMapping.getFormName());
					medMedicineMappingErr.setFormCode(medMedicineMapping.getFormCode());
					medMedicineMappingErr.setSpec(medMedicineMapping.getSpec());
					medMedicineMappingErr.setVerndor(medMedicineMapping.getVerndor());
					medMedicineMappingErr.setVerndorCode(medMedicineMapping.getId().getVerndorCode());
					medMedicineMappingErr.setApprovalNumber(medMedicineMapping.getApprovalNumber());
					medMedicineMappingErr.setId(com.gohealth.ums.util.CommonUtils.get32UUID());
					medMedicineMappingErrDao.save(medMedicineMappingErr);
				} catch (Exception e1) {
					logger.error(e1.getMessage(), e1);
					e1.printStackTrace();
				}
				errorCount++;
			} finally {
				totalCountLatch.countDown();
			}
			return errorCount;
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值