Reference: http://blog.csdn.net/madun/article/details/20313269
Create a thread pool
Executorservice service= Executors.newfixedthreadpool (nthreads);
Commit Thread:
// TODO has a return status of processing service.submit (new loaddatathread (Startdocid, Enddocid, Lucenefiledir, the. Queue,this. docs));
Thread-Safe queues:
linkedblockingqueue<document> queue=New linkedblockingqueue<document> (this. NumDocs );
Blockingqueue, as the name implies, "Blocking queue": A queue that can provide blocking functionality.
First, look at the common methods that Blockingqueue provides:
- The Add (e) Remove () Element () method does not block the thread. The illegalstateexception exception is thrown when the constraint is not met. For example: When the queue is filled with elements, then add (e) is called, and an exception is thrown.
- Offer (e) the poll () Peek () method does not block the thread and does not throw an exception. For example: When the queue is filled with an element and then call offer (e), the element is not inserted and the function returns FALSE.
- To implement blocking, you need to call the put (e) Take () method. When a constraint is not met, the thread is blocked.
|
Thread creation:
/***/Publicclassimplements Runnable
18 thread pool, threads secure queue, thread creation