Keywords: concurrency
For concurrency, Lucene. Net follows the following rules:
1. allow any number of concurrent read operations, that is, a certain number of users can perform retrieval operations on the same index at the same time.
2. Even if you are modifying indexes (index optimization, adding documents, and deleting documents), you can still execute any number of search operations concurrently.
3. concurrent modification operations are not allowed, that is, only one index modification operation is allowed at a time.
Lucene. Net has implemented multi-thread security processing. Open the indexwrite. CS/indexreade. CSR file and you will find that many operations use lock for multi-thread synchronization lock. As long as you follow certain rules, you can run Lucene. net safely in a multi-threaded environment.
Suggestion:
1. directotry and analyzer are all multi-threaded security types. You only need to create a singleton object.
2. All threads use the same indexmodifier object for index modification.
3. It is recommended that indexwriter/indexreader/indexmodifier/indexsearcher use the same directory object. Otherwise, filenotfoundexception may be triggered during multi-thread concurrent read/write.
The indexmodifier object encapsulates the common operations of indexwriter and indexreader, and implements multi-thread synchronous locking. Using indexmodifier can avoid the trouble of synchronizing multiple objects when both indexwriter and indexreader are used. After all the modifications are completed, remember to call the close () method to close related resources. Optimize () is not required for each operation. You can regularly perform optimization operations based on specific situations.
--------
The following demoCodeA simple encapsulation of an indexmodifier signleton type ensures that multiple threads use the same object, and the close method can only be disabled by the last multiple threads.
The code is not complete and is for reference only! You must make some changes to apply the changes to the actual project.
Public class myindexmodifier
{
Private Static directory = new ramdirectory ();
Private static analyzer = new standardanalyzer ();
Private Static indexmodifier modifier;
Private Static list <thread> threadlist = new list <thread> ();
Private myindexmodifier (){}
Public static indexmodifier getinstance ()
{
Lock (threadlist)
{
If (modifier = NULL)
{
Modifier = new indexmodifier (directory, analyzer, false );
Modifier. setmaxfieldlength (1000 );
}
If (! Threadlist. Contains (thread. currentthread ))
Threadlist. Add (thread. currentthread );
Return modifier;
}
}
Public static void close ()
{
Lock (threadlist)
{
If (threadlist. Contains (thread. currentthread ))
Threadlist. Remove (thread. currentthread );
If (threadlist. Count = 0)
{
If (modifier! = NULL)
{
Modifier. Close ();
Modifier = NULL;
}
}
}
}
}
Multi-thread test code
for (INT I = 0; I <100; I ++)
{< br> New thread (delegate ()
{< br> indexmodifier writer = myindexmodifier. getinstance ();
for (INT x = 0; x <10; X ++)
{< br> document DOC = new document ();
Doc. add (field. text ("A", "Hello, world! ");
writer. adddocument (DOC);
}
console. writeline ("{0 }:{ 1}", thread. currentthread. managedthreadid, writer. doccount ();
myindexmodifier. close (); // do not call indexmodifier. close ()!
}). Start ();
}