Best Way to Reuse Runnable

I have a class that implements Runnable , and I'm currently using Executor as a thread pool for running tasks (indexing documents in Lucene).

 executor.execute(new LuceneDocIndexer(doc, writer)); 

My problem is that my Runnable class creates many Lucene Field objects, and I would rather reuse them and then create new ones for each call. What is the best way to reuse these objects ( Field objects are not thread safe, so I cannot make them static) - should I create my own ThreadFactory ? I notice that after a while the program starts to deteriorate sharply, and the only thing I can think of is the GC overhead. I'm currently trying to profile the project to be sure that this is even a problem, but for now, let's just assume that it is.

+4
source share
4 answers

For the time being, I decided to just use the simple Producer-> Consumer model. I pass BlockingQueue each index, not the document for indexing, and then, so that the main program driver adds new documents to this queue. Indexers then pass this [limited] queue and reuse Field objects and exchange thread-safe IndexWriter .

I found a place where I might not have called HttpMethod.releaseConnection() , so this could cause memory problems (ambiguity).

+1
source

Your question asks how to reuse Runnable, so I will ignore the other details and just answer that question.

If you use ThreadPoolExecutor, you can use the [ThreadPoolExecutor # afterExecute] [1] method to return the Runnable to the "cached" Runnables pool / queue.

[1]: http://java.sun.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html#afterExecute(java.lang.Runnable , java.lang.Throwable)

+12
source

The Runnable is reusable. This is a stream object that is not.

The best way? this is your way :-)

I think this is a simpler question than a question that can be resolved.

+5
source

You might want to do another benchmarking to minimize what causes a slowdown.

I bet your problem is not with creating instances of Field. The field has no finalizers, and they are not intended to be combined.

+1
source

Source: https://habr.com/ru/post/1308895/


All Articles