每次同步还是构造新的

每次同步还是构造新的

本文介绍了Lucene QueryParser在多个线程中:每次同步还是构造新的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,用户可以在其中向Lucene索引提交查询.查询由Lucene解析 QueryParser .我了解到QueryParser不是线程安全的困难方法.

I have a web application where users submit queries to a Lucene index. The queriesare parsed by a LuceneQueryParser. I learned the hard way that QueryParser is not thread-safe.

使用单个QueryParser实例并同步对其parse()方法的调用是否更好?还是为每个查询构造一个新实例更好? (或者我可以最好用QueryParser s池?)

Is it better to use a single QueryParser instance, and synchronize on calls to its parse() method? Or is it better to construct a new instance for each query? (Or would I be better served by a pool of QueryParsers?)

我知道,通常这样的问题取决于具体情况,需要进行概要分析,但是也许有人可以肯定地说"QueryParser非常便宜/构造昂贵"?

I know that in general questions like this depend on the particulars and require profiling, but maybe someone out there can say definitively "QueryParsers are extremely inexpensive/expensive to construct"?

推荐答案

每次创建一个新的.这些是轻量级的对象,JVM很好地处理了对象的创建和垃圾回收.绝对不要使用对象池.

Create a new one each time. These are lightweight objects and the JVM handles object creation and garbage collection very well. Definitely do not use an object pool.

这篇关于Lucene QueryParser在多个线程中:每次同步还是构造新的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:10