Java 在规定的时间后使用managedexecutorservice关闭线程

Java 在规定的时间后使用managedexecutorservice关闭线程,java,multithreading,concurrency,ejb,executorservice,Java,Multithreading,Concurrency,Ejb,Executorservice,我有一个需要同时执行的线程列表。我最初是用executorservice做这件事的。需要执行的线程来自DB,我正在检索它们的类名,并将它们存储在vendorDetails列表中 for (Object vendorThread : vendorDetails) { String thread = (String) vendorThread; //timeout = details.getTimeout(); Runnable

我有一个需要同时执行的线程列表。我最初是用executorservice做这件事的。需要执行的线程来自DB,我正在检索它们的类名,并将它们存储在vendorDetails列表中

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(); 
        }
因此,理想情况下,我希望线程运行规定的时间,比如30秒,然后返回完成执行的线程的结果。 现在的问题是调用任何threadlifecycle方法(如shutdown等)都会引发异常。 我如何做到这一点?
我正在使用默认的ManagedExecutorSerivce。

我不知道这是否是正确的解决方案。但我现在这样做是为了解决问题。将提交的所有任务添加到未来列表中。然后等待规定的时间并取消所有正在运行的任务。我相信有一个更优雅的解决方案

                    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();
                    }
ArrayList taskList=new ArrayList();
对于(对象供应商阅读:供应商详情){
System.out.println(“in-for-loop”);
字符串线程=(字符串)vendorThread;
System.out.println(“线程:+线程”);
//timeout=details.getTimeout();
Runnable worker=null;
试一试{
if(thread.equals(“threadName1”)){
System.out.println(“in-if”);
threadName1.setReqRes(搜索请求、结果);
worker=(可运行)threadName1;
}else if(thread.equals(“threadName2”)){
System.out.println(“in-spice if”);
threadName2.setReqRes(搜索请求、结果);
worker=(Runnable)threadName2;
}
System.out.println(“在if之后”);
}捕获(例外e){
e、 printStackTrace();
}
if(worker==null){
System.out.println(“---------------------------WORKER为空------------------”);
}
System.out.println(“执行前”);
//managedExecutorService.execute(worker);
taskList.add(managedExecutorService.submit(worker));;
System.out.println(“执行后”);
}
系统输出打印项次(“关机前”);
//managedExecutorService.shutdownNow();
系统输出打印项次(“关机后”);
试一试{
系统输出打印项次(“关机后”);
/*如果(!managedExecutorService.isTerminated()){
managedExecutorService.awaitTermination(超时,时间单位为秒);
}*/
System.out.println(“睡眠前”);
long startTimeLogon=System.currentTimeMillis();
布尔全完备;
int-trueCount=0;
while(true){
System.out.println(“真计数”+trueCount++);
如果((System.currentTimeMillis()-startTimeLogon)>=超时*1000){
打破
}
全部完成=正确;
对于(未来f:任务列表){
如果(!f.isDone()){
全部完成=错误;
}
}
如果(全部完成)
打破
睡眠(250);
}
System.out.println(“睡眠后”);
对于(未来f:任务列表){
如果(!f.isDone()){
f、 取消(真);
}
}
系统输出打印项次(“取消后”);
}捕获(中断异常例外){
例如fillInStackTrace();
//例如printStackTrace();
}
                    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();
                    }