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;
		}
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章