使用ForkJoinPool执行多线程任务

Scala写的,JAVA改一下就能用

    val forkPool = new ForkJoinPool()

    val tasks = new LinkedList[Callable[Boolean]]
    tasks.add(new Callable[Boolean] {
      override def call(): Boolean = {
        val courtInfoResp = entQuerySrv.queryCourtInfo(companyName, isExactlySame = true, FIXED_PAGE_NO, FIXED_PAGE_SIZE)
        resp.courtInfo = courtInfoResp
        true
      }
    })
    tasks.add(new Callable[Boolean] {
      override def call(): Boolean = {
        val courtAnnouResp = entQuerySrv.queryCourtAnnouInfo(companyName, FIXED_PAGE_NO, FIXED_PAGE_SIZE)
        resp.courtAnnounce = courtAnnouResp
        true
      }
    })

    tasks.add(new Callable[Boolean] {
      override def call(): Boolean = {
        val distrustResp = entQuerySrv.queryEntDistrustInfo(companyName, null, isExactlySame = true, FIXED_PAGE_NO, FIXED_PAGE_SIZE)
        resp.distrustInfo = distrustResp
        true
      }
    })

    tasks.add(new Callable[Boolean] {
      override def call(): Boolean = {
        val beizhixingResp = entQuerySrv.queryEntBeiZhiXingInfo(companyName, null, isExactlySame = true, FIXED_PAGE_NO, FIXED_PAGE_SIZE)
        resp.beiZhiXingRecord = beizhixingResp
        true
      }
    })

    tasks.add(new Callable[Boolean] {
      override def call(): Boolean = {
        val courtDecisionResp = entQuerySrv.queryCourtDecisionInfo(companyName, isExactlySame = true, null, FIXED_PAGE_NO, FIXED_PAGE_SIZE)
        resp.courtDecisions = courtDecisionResp
        true
      }
    })
    forkPool.invokeAll(tasks)
    forkPool.awaitTermination(20, TimeUnit.SECONDS) //阻塞当前线程直到 ForkJoinPool 中所有的任务都执行结束  
    forkPool.shutdown()

 

你可能感兴趣的:(Java技术,Scala)