For multi-threading has been in use, but has never gone into the attention of the matter, including the lock with the mate is also smattering no real understanding of the use of locks (lock).
This time in doing data analysis is necessary to access the URL of the host part of the record, if already exist directly match the ID, no new. In order to improve the efficiency of the immediate thinking is multi-threading, because the program itself is more complex, the large length of the code.
The main idea: Lock is to lock the instance, if each thread in the thread has created a different instance the meaning of lock does not exist.
If the reference and delivery of a multi-layered class are involved, it is instantiated at the entrance of the process, requiring the initialization or invocation of the class, and passing through the layer to the business logic processing office locked operation.
If the above two sentences to understand the more abstract, then the next understanding.
Example: Now you need to do a program, and use 10 threads, each thread only needs to do a cumulative operation, when the final value equals 1000 to terminate the program.
When you implement the above example, you certainly think of the idea of defining a global variable outside of the thread, and then the same thing with the global variable lock in the threads, and the need to lock these fields in your own program makes sense.
In short, through the above-mentioned example to try to figure it out.
Project sample code (extracting critical code):
Public classcountservice{Private StaticMongoservice MDBs;//Defining global Fields PublicCountservice () {MDBs=NewMongoservice ();//Constructor Initialization } Public voidStart () {NewVisitscount (Li, _visits. Redisclient, MDBs);//calling another business class needs to pass the example over }} Public classVisitscount {Private ReadOnlyMongoservice DBS; PublicVisitscount (Mongoservice mdbs=NULL) {DBS=mdbs??NewBLL. Mongoservice (); } Public voidGet () {Lock(DBS) {//write down the query for MongoDB, update, add } } }
Multithreaded calls
thread[] Visitsanalythreads =Newthread[Ten]; for(inti =0; i < visitsanalythreads.length; i++) {Visitsanalythreads[i]=NewThread (myservice.start) {Name ="Access Analysis"+ (i +1) +"#" }; } foreach(varVaiteminchvisitsanalythreads) {Vaitem.start (); }
This example of MONGDB is passed through the layer layer to eventually implement the normal operation of multithreading.
In addition, MONGDB itself is a support lock, but the C # Mongodb.driver did not do things to support it.
Available through database. Requeststart ();d atabase. Requestdone (); But this operation knowledge solves mongdb concurrency problem, cannot solve the problem which the multithreading above causes.
C # Multithreading Update, querying MONGODB data