Persistent Variables in ExecutorService (Java)

后端 未结 3 545
Happy的楠姐
Happy的楠姐 2021-01-18 17:09

I\'ve created a pool of 4 worker threads to process some files. In the test there\'s about 200 of them. The thread pool version is already about 3 times faster than doing it

相关标签:
3条回答
  • 2021-01-18 17:42

    You can use an object pool for your message digest objects. And set the pool size to 4 in your case.

    apache commons provides excellent pool api: http://commons.apache.org/pool/

    0 讨论(0)
  • 2021-01-18 17:43

    Extend ThreadLocal for your class and override the initialValue() method. By default, it returns null.

    private static class ThreadLocalGenerateSHA1 extends
      ThreadLocal<GenerateSHA1> {
    
      @Override
      protected GenerateSHA1 initialValue() {
        return new GenerateSHA1();
      }
    
    }
    
    private static final ThreadLocalGenerateSHA1 generateSHA1 = new ThreadLocalGenerateSHA1();
    
    ...
    

    On the tasks, simply call the get() method of generateSHA1. You do not need to call set().

    0 讨论(0)
  • 2021-01-18 17:56

    You can take help of beforeExecute and afterExecute methods of the ThreadPoolExecutor by extending it. In your extended class, create 4 MessageDigest objects and assign them to each task in beforeExecute(...) in a round-robin fashion.

    private static final Executor executor = new MyThreadPoolExecutor(10, 10, 50000L,   TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(100));
    

    EDIT

    I feel the object pool option mentioned by Ravi Bhatt as the simple and elegant idea. Create a pool and let the task ask for the MessageDigest from the pool.

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