0

Need to stop all child threads if not all threads are finished within given a parent thread timeout

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();



    }
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
naveen
  • 1
  • 1
  • You cannot stop thread in java. You can try to interrupt it but the task that is executed in that thread should be responsive to interruption. – Ivan Jan 18 '19 at 17:51
  • 1
    Possible duplicate of [Killing child thread from parent thread in java](https://stackoverflow.com/questions/17123245/killing-child-thread-from-parent-thread-in-java) – Sahil Dhoked Jan 18 '19 at 17:54
  • There is no such thing as 'child' threads in Java. – Mark Rotteveel Jan 19 '19 at 12:15

0 Answers0