When a thread enters an synchronized method of an object, can other threads enter other methods of this object? __java Multithreading

Source: Internet
Author: User

The Synchronized method of an object cannot be entered, but its other synchronized methods are accessible.

Let's take a look at an example:

Package dhp.test1;

public class TT implements Runnable {
     int b = m;
     Public synchronized void M1 () {
            b = 10000;
            try {
                Thread. Sleep (5000);
                System. OUT.PRINTLN ("m1----b=" + b);
           } catch (Interruptedexception e) {
                e.printstacktrace ();
           }
     }
     public void m2 () {
           System. Out.println ("m2=====" + b);
     }
     @Override public
     Void Run () {
           m1 ();
     }
     public static void Main (string[] args) {
           tt tt = new TT ();
           Thread t = new thread (TT);
           T.start ();
            try {
                Thread. Sleep (1000);
           } catch (Interruptedexception e) {
                e.printstacktrace ();
           }
           TT.M2 ();
     }



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.