2017-02-10 28 views
0

我的目標是限制處理大文件時的內存使用情況。 要做到這一點,我使用了一個線程池實現,它應該使它不可能從文件中加載更多的數據,然後在給定的時間處理它。線程池RejectedExecutionHandler它是如何工作的

try (CSVParser parser = new CSVParser(new File("...."))) { 
    ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 5, TimeUnit.MINUTES, new ArrayBlockingQueue<>(1), new RejectedExecutionHandler() { 
     @Override 
     public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { 
      r.run(); 
     } 
    }); 

    for (Item item; (item = parser.nextItem()) != null;) { 
     executor.submit(new ItemsProcessor(item)); 
    } 

    executor.shutdown(); 
    executor.awaitTermination(12, TimeUnit.HOURS); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

我的理解是,RejectedExecutionHandlerrejectedExecution方法將主線程上運行,其上ThreadPoolExecutor創建的線程。是這樣嗎 ?

被拒絕的任務是否在創建線程池的同一線程上運行?

據我所知,這種方法只能在內存中加載最多12個項目。 10正在被線程池處理,一個在線程池的隊列中,一個被拒絕,並且在與循環相同的線程上運行(暫停循環)。

回答

0

RejectExecutionHandler是在第一個執行程序停止工作的情況下提供的工具。

如果第一個執行程序拒絕線程執行,RejectExecutionHandler將被調用。

// Create Executor for Normal Execution 
    public static ThreadPoolExecutor executor=(ThreadPoolExecutor) Executors.newFixedThreadPool(10); 

    // Create Executor for Alternate Execution in case of first Executor shut down 
    public static ThreadPoolExecutor alternateExecutor=(ThreadPoolExecutor) Executors.newFixedThreadPool(10); 

/Create Rejected ExecutionHandler Class override rejectedExecution method 
public class MyRejectedExecutionHandler implements RejectedExecutionHandler { 



    @Override 
    public void rejectedExecution(Runnable worker, ThreadPoolExecutor executor) { 
     // TODO Auto-generated method stub 
     System.out.println(worker.toString()+" is Rejected"); 

     System.out.println("Retrying to Execute"); 
     try{ 
      //Re-executing with alternateExecutor 
      RejectedExecutionHandlerExample.alternateExecutor.execute(worker); 
      System.out.println(worker.toString()+" Execution Started"); 
     } 
     catch(Exception e) 
     { 
      System.out.println("Failure to Re-exicute "+e.getMessage()); 
     } 
    } 

} 



// Register RejectedExecutionHandler in Main Class 
RejectedExecutionHandler handler=new MyRejectedExecutionHandler(); 

executor.setRejectedExecutionHandler(handler); 
+0

我知道了什麼'RejectedExecutionHandler'用於什麼,我不確定的是它的'rejectedExecution'方法將被調用到哪個線程上。 – Titus