2016-07-12 2 views
2

동시에 실행해야하는 스레드 목록이 있습니다. 처음에는 executorservice를 사용하여이 작업을 수행했습니다. 실행될 필요가있는 쓰레드는 DB에서 나오고 클래스 네임을 가져 와서 vendorDetails리스트에 저장합니다.규정 된 시간 후에 managedexecutorservice를 사용하는 종료 스레드

for (Object vendorThread : vendorDetails) { 
      String thread = (String) vendorThread; 
      //timeout = details.getTimeout(); 
      Runnable worker = null; 
      try { 
       Class c = Class.forName(thread); 
       Constructor<?> cons = c.getConstructor(SearchRequest.class, Results.class); 
       worker = (Runnable) cons.newInstance(searchRequest, Results); 
      } catch (Exception e) { 
       //e.printStackTrace(); 
      } 
      if (worker == null) { 
       System.out.println("------------------------ WORKER IS NULL ---------------"); 
      } 
      executor.execute(worker); 
     } 
     executor.shutdownNow(); 
     try { 
      if (!executor.isTerminated()) { 
       executor.awaitTermination(timeout, TimeUnit.SECONDS); 
      } 

     } catch (InterruptedException ex) { 
      //ex.fillInStackTrace(); 
     } 

은 내가 ManagedExecutorService을 사용하고, 그래서 EJB에서 비슷한 일을 달성하고 싶다.

@EJB 
    private ThreadName1 threadName1 ; 
    @EJB 
    private ThreadName2 threadName2 ;  
for (Object vendorThread : vendorDetails) { 
       System.out.println("in for loop"); 
       String thread = (String) vendorThread; 
       System.out.println("thread:" + thread); 
       //timeout = details.getTimeout(); 
       Runnable worker = null; 
       try { 
        if (thread.equals("threadName1")) { 
         System.out.println("in if"); 
         threadName1.setReqRes(SearchRequest, Results); 
         worker = (Runnable) threadName1; 
        } else if (thread.equals("threadName2")) { 
         System.out.println("in spice if"); 
         threadName2.setReqRes(SearchRequest, Results); 
         worker = (Runnable) threadName2; 
        } 
        System.out.println("after if"); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       if (worker == null) { 
        System.out.println("------------------------ WORKER IS NULL ---------------"); 
       } 

       System.out.println("before execute"); 
       //managedExecutorService.execute(worker); 
       managedExecutorService.submit(worker); 
       System.out.println("after execute"); 
      } 
      System.out.println("before shutdown"); 
      //managedExecutorService.shutdownNow(); 
      System.out.println("after shutdown"); 
      try { 
       System.out.println("after shutdown"); 
       /*if (!managedExecutorService.isTerminated()) { 
        managedExecutorService.awaitTermination(timeout, TimeUnit.SECONDS); 
       }*/ 

      } catch (InterruptedException ex) { 
       ex.fillInStackTrace(); 
       // ex.printStackTrace(); 
      } 

그래서 이상적으로 나는 스레드, 30 초를 말하는 규정 시간 동안 실행 한 다음 스레드가 실행을 완료 한 쪽의 결과를 반환합니다. 이제 shutdown 등의 threadlifecycle 메서드를 호출하면 예외가 throw됩니다. 어떻게해야합니까? 기본 ManagedExecutorSerivce를 사용하고 있습니다.

답변

0

이것이 올바른 해결책인지 잘 모릅니다. 그러나 나는 이것을 해결 방법으로 현재하고있다. 제출 된 모든 작업을 미래 목록에 추가합니다. 그런 다음 규정 된 시간 동안 기다리고 모든 실행중인 작업을 취소합니다. 나는 더 우아한 해결책이있을 것이라고 확신한다.

    ArrayList<Future> taskList = new ArrayList<>();   
        for (Object vendorThread : vendorDetails) { 
         System.out.println("in for loop"); 
         String thread = (String) vendorThread; 
         System.out.println("thread:" + thread); 
         //timeout = details.getTimeout(); 
         Runnable worker = null; 
         try { 
          if (thread.equals("threadName1")) { 
           System.out.println("in if"); 
           threadName1.setReqRes(SearchRequest, Results); 
           worker = (Runnable) threadName1; 
          } else if (thread.equals("threadName2")) { 
           System.out.println("in spice if"); 
           threadName2.setReqRes(SearchRequest, Results); 
           worker = (Runnable) threadName2; 
          } 
          System.out.println("after if"); 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 
         if (worker == null) { 
          System.out.println("------------------------ WORKER IS NULL ---------------"); 
         } 

         System.out.println("before execute"); 
         //managedExecutorService.execute(worker); 
         taskList.add(managedExecutorService.submit(worker));; 
         System.out.println("after execute"); 
        } 
        System.out.println("before shutdown"); 
        //managedExecutorService.shutdownNow(); 
        System.out.println("after shutdown"); 
        try { 
         System.out.println("after shutdown"); 
         /*if (!managedExecutorService.isTerminated()) { 
          managedExecutorService.awaitTermination(timeout, TimeUnit.SECONDS); 
         }*/ 
       System.out.println("before sleep"); 
       long startTimeLogon = System.currentTimeMillis(); 
       boolean allComplete; 
       int trueCount = 0; 
       while (true) { 
        System.out.println("true count " + trueCount++); 
        if ((System.currentTimeMillis() - startTimeLogon) >= timeout * 1000) { 
         break; 
        } 
        allComplete = true; 
        for (Future f : taskList) {      
         if (!f.isDone()) { 
          allComplete=false; 
         } 
        } 
        if(allComplete) 
         break; 
        Thread.sleep(250); 
       } 
       System.out.println("after sleep"); 
       for (Future f : taskList) { 
        if (!f.isDone()) { 
         f.cancel(true); 
        } 
       } 
       System.out.println("after cancel"); 

        } catch (InterruptedException ex) { 
         ex.fillInStackTrace(); 
         // ex.printStackTrace(); 
        }