Persistent variables in ExecutorService (Java)

I created a pool of 4 worker threads to process some files. There are about 200 in the test. The version of the thread pool is already about 3 times faster than sequentially, but there are opportunities for improvement.

The biggest bottleneck (ignoring disk I / O) is the need to create a new MessageDigest object. In the single-threaded version, I had only 1. In this version, I have 200.

I was wondering if it is possible to have a local variable for a thread in a working pool? That way (assuming no threads die) there will be only four instances of the MessageDigest object, not 200 ...

Each job requires a digest, so I'm not sure if there is a better way to do this ...

UPDATE

I tried to use a ThreadLocal object, but where can I create it? If I create it in the task itself, I think it will go out of context when the task is completed. Every time a new instance is created. The code I have is:

    ThreadLocal<GenerateSHA1> tl = new ThreadLocal<GenerateSHA1>();

    hashMaker = tl.get();
    if(hashMaker == null){
        hashMaker = new GenerateSHA1();
        tl.set(hashMaker);
    }

This is done from within the task constructor.

UPDATE

Ok, let's do some static work since the object is not lost - but now it has highlighted another problem. Worker tasks are created in the main thread and then added to the ExecutorService using invokeAll ().

Any ideas on how to get around this?

+5
source share
3 answers

Extend ThreadLocalfor your class and override the method initialValue(). 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();

...

get() generateSHA1. set().

+3

beforeExecute afterExecute ThreadPoolExecutor . 4 MessageDigest beforeExecute (...) .

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

, . MessageDigest .

0

. 4 .

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

0
source

All Articles