동시에 실행해야하는 스레드 목록이 있습니다. 처음에는 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를 사용하고 있습니다.