First, preface
I would like to say in another article, found that may be a bit large, so it is a separate blog to say good. Knowledge Reference "Java multithreaded Programming core technology", evaluation of the book Bar--A large number of code, simple explanation, really like looking at the blog. But this book is easy to understand, not long, take a breath of dozens of pages, and then hit the code with the demo, it is not too cool. Ha ha
Second, the concept
Object Lock: as the name implies, this lock belongs to an object instance of this class, which can be synchronized by adding a keyword to a non-static method in a class or using the synchronized (this) block of code. Adds an object lock to the program.
class Lock: as the name implies, this lock belongs to this class, so even though different instance objects still have the same lock, you can add the Synchronized keyword to the static method in the class or use the Synchronized (*. Class) code block to add class locks to the program.
Third, code description
Public classTask {//class Lock synchronized Public Static voidPrintA () {Try{System.out.println ("Thread Name:" + Thread.CurrentThread (). GetName () + "in" + system.currenttimemillis () + "Enter Printa"); Thread.Sleep (3000); System.out.println ("Thread Name:" + Thread.CurrentThread (). GetName () + "in" + system.currenttimemillis () + "Leave Printa"); } Catch(interruptedexception e) {e.printstacktrace (); } } //class Lock Public Static voidPrintb () {Try { synchronized(Task.class) {//class locks defined in this waySystem.out.println ("Thread Name:" + Thread.CurrentThread (). GetName () + "in" + system.currenttimemillis () + "Enter PRINTB"); Thread.Sleep (3000); System.out.println ("Thread Name:" + Thread.CurrentThread (). GetName () + "in" + system.currenttimemillis () + "Leave Printb"); } } Catch(interruptedexception e) {e.printstacktrace (); } } //Object Lock synchronized Public voidPrintc () {Try{System.out.println ("Thread Name:" + Thread.CurrentThread (). GetName () + "in" + system.currenttimemillis () + "Enter PRINTC"); Thread.Sleep (3000); System.out.println ("Thread Name:" + Thread.CurrentThread (). GetName () + "in" + system.currenttimemillis () + "Leave Printc"); } Catch(interruptedexception e) {e.printstacktrace (); } } //Object Lock Public voidPrintd () {Try { synchronized( This){//object locks defined in this waySystem.out.println ("Thread Name:" + Thread.CurrentThread (). GetName () + "in" + system.currenttimemillis () + "Enter Printd"); Thread.Sleep (3000); System.out.println ("Thread Name:" + Thread.CurrentThread (). GetName () + "in" + system.currenttimemillis () + "Leave Printd"); } } Catch(interruptedexception e) {e.printstacktrace (); } }}
Iv. Summary
1. Class lock and Object lock belong to different locks, belong to asynchronous execution, there is scramble effect.
2. Class lock locks the class class corresponding to the current *.java file, and it works on all instance objects of the class. An object lock only works on its own instance object.
Analysis of multi-threaded object lock and Class lock