Java multi-thread in-depth understanding of Synchronize keywords

Source: Internet
Author: User

Synchronize lock re-entry:

The keyword synchronize has the function of lock-in, that is, when using synchronize, when a thread gets a lock on an object, requesting again this object is a lock that can get the object again.
When a thread requests a lock held by another thread, the thread that makes the request is blocked, however, because the built-in lock is reentrant, if a thread attempts to acquire a lock that is already held by herself, the request succeeds and "re-enter" means that the granularity of the operation that acquires the lock is "thread", not the call.

PublicClassSyncdubbol {PublicSynchronizedvoidMethod1 () {System.out.println ("Method1 ..."); Method2 (); }PublicSynchronizedvoid method2 () {System.out.println ( public synchronized void method3 () {System.out.println (public static void main (string[] args) {final syncdubbol sd=new Syncdubbol (); new Thread (new Runnable () {@ Override public void run () { Sd.method1 (); }}). Start (); }} 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24

The above code, although three methods are synchronize decorated, but in the execution of the Method1 method has been the lock of the object, so in the execution of the Method2 method without waiting, directly obtain the lock, so the result of this method is

method1....method2....method3....
    • 1
    • 2
    • 3

If the built-in lock is not reentrant, then this code will have a deadlock.

public class widget{public synchronized void dosomething () { System.out.println ( "dosomthing");}} public class loggingwidget extends Widget{public synchronized void dosomething () {System.out.println (" calling dosomthing "); super.dosomething (); }} 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

Java multi-thread in-depth understanding of Synchronize keywords

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.