在规定的时间

时间:2016-07-12 07:14:48

标签: java multithreading concurrency ejb executorservice

我有一个需要同时执行的线程列表。我最初使用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(); 
        }

我想在ejb中实现类似的功能,因此我使用的是ManagedExecutorService

    @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等会引发异常。 我该如何实现这一目标? 我使用的是默认的ManagedExecutorSerivce。

1 个答案:

答案 0 :(得分: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();
                    }