GoodnessOfFitThread.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package com.gyee.generation.task.thread;
  2. import com.gyee.generation.model.auto.ProEconEquipmentInfoDay1;
  3. import com.gyee.generation.model.auto.ProEconWindturbineGoodness;
  4. import com.gyee.generation.service.CoefficientService;
  5. import com.gyee.generation.service.GoodnessOfFitService;
  6. import com.gyee.generation.service.auto.ITurbineInfoMinService;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import java.util.Date;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.concurrent.CountDownLatch;
  13. import java.util.concurrent.ExecutionException;
  14. import java.util.concurrent.Executor;
  15. import java.util.concurrent.FutureTask;
  16. public class GoodnessOfFitThread implements Runnable {
  17. private Logger logger = LoggerFactory.getLogger(this.getClass());
  18. private Executor executor;
  19. private int i;
  20. private Date recordDate;
  21. private CoefficientService coefficientService;
  22. private GoodnessOfFitService goodnessOfFitService;
  23. private Map<String, ProEconEquipmentInfoDay1> equipmentInfoDay1Map;
  24. private List<ProEconWindturbineGoodness> vos;
  25. private CountDownLatch latch;
  26. private ITurbineInfoMinService turbineInfoMinService;
  27. public GoodnessOfFitThread(Executor executor, int i, Date recordDate, CoefficientService coefficientService,
  28. GoodnessOfFitService goodnessOfFitService,
  29. ITurbineInfoMinService turbineInfoMinService, Map<String, ProEconEquipmentInfoDay1> equipmentInfoDay1Map, List<ProEconWindturbineGoodness> vos,
  30. CountDownLatch latch) {
  31. this.executor = executor;
  32. this.i = i;
  33. this.recordDate = recordDate;
  34. this.coefficientService = coefficientService;
  35. this.goodnessOfFitService = goodnessOfFitService;
  36. this.equipmentInfoDay1Map = equipmentInfoDay1Map;
  37. this.turbineInfoMinService = turbineInfoMinService;
  38. this.vos = vos;
  39. this.latch = latch;
  40. }
  41. @Override
  42. public synchronized void run() {
  43. String task = "task@ " + i;
  44. logger.info("功率一致性系数创建任务并提交到线程池中:" + i);
  45. FutureTask<String> futureTask = new FutureTask<String>(new GoodnessOfFitThreadPool(task, i, recordDate, coefficientService,
  46. goodnessOfFitService,turbineInfoMinService,
  47. equipmentInfoDay1Map, vos));
  48. executor.execute(futureTask);
  49. // 在这里可以做别的任何事情
  50. String result = null;
  51. try {
  52. // 取得结果,同时设置超时执行时间为1秒。同样可以用future.get(),不设置执行超时时间取得结果
  53. result = futureTask.get();
  54. } catch (InterruptedException e) {
  55. futureTask.cancel(true);
  56. } catch (ExecutionException e) {
  57. futureTask.cancel(true);
  58. } catch (Exception e) {
  59. futureTask.cancel(true);
  60. // 超时后,进行相应处理
  61. } finally {
  62. latch.countDown();
  63. }
  64. }
  65. }