java如果给定父线程超时未完成所有线程,则需要停止所有子线程

时间:2019-01-18 17:49:33

标签: java multithreading

如果不是在给定父线程超时的情况下所有线程都未完成,则需要停止所有子线程

final ExecutorService threadPool = Executors.newFixedThreadPool(2);
        final ExecutorCompletionService<Object> completionService = new ExecutorCompletionService<>(
                threadPool);

        List<Future> futures = new ArrayList<>();
        //launching child thread
        futures.add(completionService.submit(new Runnable() {

            @Override
            public void run() {
//launching more threads inside this thread with new executor service
                final ExecutorService threadPool = Executors.newFixedThreadPool(2);
                final ExecutorCompletionService<Object> completionService = new ExecutorCompletionService<>(
                        threadPool);

                List<Future> futures = new ArrayList<>();
//thread1
                futures.add(completionService.submit(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("Thead1 :" + Thread.currentThread().getId());
                    }
                },null));
thread2
                futures.add(completionService.submit(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Thread.sleep(20000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("Thead2 :" + Thread.currentThread().getId());

                    }
                },null));

                threadPool.shutdown();

            }
        },null));


        ////Need to stop all child threads with single time out with parent thread

        while(futures.size() > 0) {
            System.out.println("Master pool Waiting started...");
             try{
            Future ft = completionService.poll(1000, TimeUnit.MILLISECONDS);


            if(null !=ft) {
                Object task = ft.get();
                futures.remove(ft);
            }else {
                new Exception("Failed processing in time..., killing threds");
            }
            }catch(InterruptedException e){
                e.printStackTrace();
                // Interrupted
            }catch(ExecutionException e){
                // Snap, something went wrong in the task! Abort! Abort! Abort!
                e.printStackTrace();
                for(Future f : futures){
                    f.cancel(true);
                    if(f.isCancelled()) {
                        System.out.println("Task :"+f.toString()+" cancelled");
                    }else {
                        System.out.println("Task :"+f.toString()+" Not cancelled");
                    }

                }
                futures.clear();
            }   
        }
        threadPool.shutdownNow();



    }

0 个答案:

没有答案