Analysis of multi-threaded object lock and Class lock

Source: Internet
Author: User

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

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.