重用Runnable的最佳方式 [英] Best way to reuse a Runnable
问题描述
我有一个类实现 Runnable
,我目前使用Executor作为我的线程池运行任务(索引文件到Lucene)。
I have a class that implements Runnable
and am currently using an Executor as my thread pool to run tasks (indexing documents into Lucene).
executor.execute(new LuceneDocIndexer(doc, writer));
我的问题是我的Runnable类创建了许多Lucene 字段
对象和I宁愿重用它们,然后在每次调用时创建新的。重用这些对象的最佳方式是什么( Field
对象不是线程安全所以我不能简单使它们静态) - 我应该创建自己的 ThreadFactory
?我注意到,一段时间后,程序开始严重退化,我唯一能想到的是它的GC开销。我目前正在尝试剖析该项目,以确保这是一个问题 - 但现在让我们假设它是。
My issue is that my Runnable class creates many Lucene Field
objects and I would rather reuse them then create new ones every call. What's the best way to reuse these objects (Field
objects are not thread safe so I cannot simple make them static) - should I create my own ThreadFactory
? I notice that after a while the program starts to degrade drastically and the only thing I can think of is it's GC overhead. I am currently trying to profile the project to be sure this is even an issue - but for now lets just assume it is.
推荐答案
现在我决定只使用一个简单的Producer-> Consumer模型。我向每个索引器传递 BlockingQueue
,而不是将文档索引,然后让程序的主驱动程序向该队列添加新文档。索引器然后将该有限队列取出并重用 Field
对象并共享线程安全 IndexWriter
。
For now I have decided to just use a simple Producer->Consumer model. I pass a BlockingQueue
to each indexer, rather then a document to index, and then have the main driver of the program add new documents to that queue. The Indexers then feed off that [bounded] queue and reuse the Field
objects and share the thread-safe IndexWriter
.
我找到一个地方,我可能不会调用 HttpMethod.releaseConnection()
问题(不确定)。
I did find a place where I was possibly not calling HttpMethod.releaseConnection()
so that could have caused my memory issues (uncertain).
这篇关于重用Runnable的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!