Analysis of multi-thread object lock and Class lock, analysis of multi-thread class lock

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.