Java Multithreading Basics Summary VI: synchronized (2)

Source: Internet
Author: User
Tags sleep sleep function thread

As early as the summary, I will try to put the focus of the synchronized is simple: it is matching and object of the implicit lock use, attention must be the object of the implicit lock! So how does the following example explain this?

Java code

/**
* User:yanxuxin
* Date:dec 17, 2009
* Time:9:38:27 PM
*/
public class Implicitlocksample {

public static void Main (string[] args) {
Final Implicitlock sample = new Implicitlock ();

New Thread (New Runnable () {
public void Run () {
Implicitlock.method1 ();
Sample.method1 ();
}
). Start ();

New Thread (New Runnable () {
public void Run () {
SAMPLE.METHOD2 ();
}
). Start ();
}
}

Class Implicitlock {

public static synchronized void Method1 () {
SYSTEM.OUT.PRINTLN ("method1 executing ...");
try {
Thread.Sleep (3000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
}

Public synchronized void Method2 () {
SYSTEM.OUT.PRINTLN ("method2 executing ...");
try {
Thread.Sleep (3000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}

Here Implicitlock has two synchronous methods, one is static, the other is normal. Implicitlocksample is a test main program that instantiates a Implicitlock object and turns on two threads, each calling the object's Method1 and Method2 methods respectively. Each thread that enters the method forces hibernation for 3 seconds. So what is the implementation of the phenomenon?

To know the answer to the following points to be clear: a. The relationship between class and object, the meaning of B.static method, the c.synchronized mechanism, the D.sleep function. With a clear understanding of these, one can discern the synchronized of Method1 and Method2 methods and the implicit locking of two different objects. The answer is also clear that the two threads execute a print statement that does not differ nearly 3 seconds of waiting, but almost at the same time. Let me try to explain.

Class is a subclass of object, which shows that class is a special object and that it naturally has an implicit lock on an object. The static declaration method means that the method does not depend on an instance of the class, but rather as a method of the class object that removes the implicit argument this. Synchronized is bound to an object implicitly, which means that it is bound to the holding object of the method by placing it on the method declaration. Therefore, the METHOD1 synchronization lock is an implicit lock of the Implicitlock class object, and the METHOD2 synchronization lock is an implicit lock of the Implicitlock instance object. Sleep allows the current thread to hibernate, but it does not release the implicit lock it holds. As a result, the main program executes that although the same instance is used to allow two threads to call two methods separately, there is no competitive lock between them, so printing almost at the same time will not be nearly 3 seconds apart. It is easier to understand that changing a method1 call to a commented code. If the synchronized of the method1 is removed, or METHOD2 plus the synchronized declaration, they will compete for the same implicit lock. The thread that acquired the lock first will surrender the lock in 3 seconds before the thread can perform the printing.

Writing this addendum stems from a new understanding of the lazy single example, when the mechanism of the synchronized is not clear, only know that using the Synchronized keyword in the static method declaration can guarantee a single case of thread safety, but do not know that it is a mistake to understand. Prior to constructing this validation example, the common use of static and synchronized gave me a clearer understanding of the synchronized implicit lock. So I'm going to write and share this experience.

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.