Analysis of multi-thread object lock and Class lock, analysis of multi-thread class lock
I. Preface
I originally wanted to talk about it in another article. I found it may be a little too long, so I 'd better open another blog post. For more information, see Java multi-thread programming core technology. evaluate this book-a large amount of code and a simple description. It is like reading a blog. However, this book is easy to understand and does not take a long time. You can read dozens of pages in one breath and then tap on the Code according to the demo .. Haha
Ii. Concepts
Object lock:As the name suggests, this lock belongs to the object instance of this class. You can add the synchronized keyword to the non-static method in the class or use the synchronized (this) code block to add an object lock to the program.
Class lock:As the name suggests, this lock belongs to this Class, so even different instance objects still have the same lock. You can add the synchronized keyword to the static method in the Class or use synchronized (*. class) code block, which adds a Class lock to the program.
Iii. Code Description
Public class Task {// Class lock synchronized public static void printA () {try {System. out. println ("Thread name:" + Thread. currentThread (). getName () + "in" + System. currentTimeMillis () + "Enter printA"); Thread. sleep (1, 3000); System. out. println ("Thread name:" + Thread. currentThread (). getName () + "in" + System. currentTimeMillis () + "Exit printA");} catch (InterruptedException e) {e. printStackTrace () ;}// Class lock public static void printB () {try {synchronized (Task. class) {// The Class lock System defined in this way. out. println ("Thread name:" + Thread. currentThread (). getName () + "in" + System. currentTimeMillis () + "Enter printB"); Thread. sleep (1, 3000); System. out. println ("Thread name:" + Thread. currentThread (). getName () + "in" + System. currentTimeMillis () + "Exit printB");} catch (InterruptedException e) {e. printStackTrace () ;}/// object lock synchronized public void printC () {try {System. out. println ("Thread name:" + Thread. currentThread (). getName () + "in" + System. currentTimeMillis () + "Enter printC"); Thread. sleep (1, 3000); System. out. println ("Thread name:" + Thread. currentThread (). getName () + "in" + System. currentTimeMillis () + "Exit printC");} catch (InterruptedException e) {e. printStackTrace () ;}/// object lock public void printD () {try {synchronized (this) {// this method defines the object lock System. out. println ("Thread name:" + Thread. currentThread (). getName () + "in" + System. currentTimeMillis () + "Enter printD"); Thread. sleep (1, 3000); System. out. println ("Thread name:" + Thread. currentThread (). getName () + "in" + System. currentTimeMillis () + "Exit printD");} catch (InterruptedException e) {e. printStackTrace ();}}}
Iv. Summary
1. The Class locks and Object locks belong to different locks and belong to asynchronous execution. They compete for each other.
2. The Class lock locks the Class corresponding to the current *. java file, and applies to all instance objects of this Class. The object lock only applies to the instance objects of the current user.