In order to implement a function, in the tree structure, when modifying a node name, it is necessary to modify the node full path attribute field of all child nodes of this node (similar to the construction of parent node name/parent node name/child node name/double node name). Because in the construction of the UPDATE statement, it is necessary to recursively query the child nodes and construct the value of the full path, it takes a certain amount of time, such as the batch execution of the UPDATE statement, the name of a child node may be changed, it will cause conflict, it is used to synchronized, By the way, the realization of the next way, the principle is not in-depth understanding, this record for later review.
-----------------------------------------------------I am the text split line--------------------I am the body dividing line---------------------------
This article mainly lists the different synchronization methods, as well as my understanding of the scope of use.
As we all know, synchronization is divided into two usages: synchronous method and synchronous block.
One, synchronous block
Class 1.1 Synchronization
· Implementation method:
1 Public classJUSTPLAYSERVICEIMPLSYN2 {2 3 PublicJustPlayServiceImplSyn2 () {4 5 }6 Private Static intFlag = 1;7 Public voidoperate () {8 synchronized(JUSTPLAYSERVICEIMPLSYN2.class){9flag++; Ten Try { One //increase randomness so that different threads can perform alternately here AThread.Sleep (NewRandom (). Nextint (5)); -}Catch(interruptedexception e) { - e.printstacktrace (); the } -flag--; -System.out.println ("Thread:" +Thread.CurrentThread (). GetName () -+ "/current flag:" +flag); + } - } +}View Code
· Test code:
1 Public classJustplay {2 /**3 * @paramargs4 */5 Public Static voidMain (string[] args) {6 NewThread ("Thread-01") { 7 Public voidrun () {8 NewJustPlayServiceImplSyn2 (). operate ();9 } Ten}.start ();//start the first thread One A NewThread ("Thread-02") { - Public voidrun () { - NewJustPlayServiceImplSyn2 (). operate (); the } -}.start ();//start the first thread - } -}View Code
· Scope of Use:
Using the widest range, multithreaded multi-object synchronization, the code is also simple, in the need to synchronize the code block with the Synchronized keyword, and in the parentheses class with [class name] class.
-----------------------------------------too late to add--unfinished line-------------------
1.2 This sync
1.3 Static Object Synchronization
Second, the synchronization method
2.1 Static method synchronization
2.2 Non-static method synchronization
Java synchronized synchronization in different ways