Java: Force stopping of ExecutorService threads

后端 未结 6 699
天涯浪人
天涯浪人 2020-12-17 00:44

My code:


String[] torrentFiles = new File(\"/root/torrents/\").list();

        if(torrentFiles.length == 0 || torrentFiles == null)
        {
            S         


        
相关标签:
6条回答
  • 2020-12-17 01:09

    Since downloading a torrent probably involves blocking IO operations, simply calling cancel()/shutdownNow() won't be enough, because blocking IO operations are not guaranteed to terminate when their respective threads are interrupted.

    You also need to close the underlying sockets in order to cancel blocking IO, see How to terminate a thread blocking on socket IO operation instantly?.

    0 讨论(0)
  • 2020-12-17 01:12

    ExecutorService.submit(...) returns a Future<?> that has a cancel() method. You should keep track of these can call it when you want each task to stop.

    0 讨论(0)
  • 2020-12-17 01:14

    Now I have to stop threads from a pool. I am doing it such a way. It may be not a good idea. Comment, please, if so.

    boolean isTerminated = mPoolThreads.isTerminated();
    while (!isTerminated) {
        mPoolThreads.shutdownNow();
        isTerminated = mPoolThreads.isTerminated();
        Log.i(Constants.LOG_TAG, "Stop threads: the threads are not terminated yet");
    }
    Log.w(Constants.LOG_TAG, "Stop threads: Terminated");
    
    0 讨论(0)
  • 2020-12-17 01:19

    Instant thread termination is never guaranteed, unless the thread checks periodically for isInterrupted() flag (or is waiting in interruptable method, i.e. which throws InterruptedException).

    Consider implementing your worker threads in manner, when they check periodically for isInterrupted(). This may be something like that:

    public void run() { 
      byte[] data;
      do {
         data = receiveDataChunk(timeout);
         processData(data);
      } while(!isInterrupted() && data != null);
    }
    
    0 讨论(0)
  • 2020-12-17 01:23

    Am Using this code i have created.

    Its generating many pdf files from many html templates using wkhtmltopdf .

    so i want to increase performance of creating handreds without keep client waiting, this is only one part of implementation.

    About getListOfCallables its returning the correct optimal threshold for # of threads to use in fixed pool creation.

    So i cant handle having lots of un dead threads laying around it made my EC2 CPU 100% stuck.

    I used :

    • shutdown()
    • shutdownNow() in else of await
    • shutdownNow() in exception part

    List fileGenerationHtmlToPdfList = getListOfCallables(paths, name, options);

                ExecutorService executorService = Executors.newFixedThreadPool(fileGenerationHtmlToPdfList.size());
    
    
                List<Future<ArrayList<File>>> futures = null;
    
                try {
                    futures = executorService.invokeAll(fileGenerationHtmlToPdfList);
                    try {
                        for(Future f: futures) {
                            files.addAll((ArrayList<File>)f.get());
                        }
    
                    } catch (InterruptedException ex) {
                        Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE, "Interrupted Exception " , ex);
                    } catch (ExecutionException ex) {
                        Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE, "Interrupted Exception " , ex);
                    }
                } catch (InterruptedException ex) {
                    Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE, "Interrupted Exception " , ex);
                }
    
     executorService.shutdown();//try shutdown
    
                try {
                    if (executorService.awaitTermination(5, TimeUnit.SECONDS)) {
                        Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE, "Done ShutDowned");
                    } else {
                        executorService.shutdownNow();
                    }
                } catch (InterruptedException ex) {
                    executorService.shutdownNow();
                    Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE, "Interrupted Exception " , ex);
                }
    
    0 讨论(0)
  • 2020-12-17 01:32

    ExecutorService.shutdownNow() will try to stop all the executing threads..

    Here is a quote from javadoc

    List<Runnable> shutdownNow()

    Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

    There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so if any tasks mask or fail to respond to interrupts, they may never terminate.

    0 讨论(0)
提交回复
热议问题