The difference between synchronous method and synchronous block

Source: Internet
Author: User
Tags instance method sleep

Java uses synchronized synchronization in four scenarios:

 instance method synchronization

The synchronization block in the  instance method

 static method synchronization

Synchronous block in the  static method


We are different from two aspects, one is the synchronization method and the synchronization block difference, one is static and non-static difference.

The synchronous method is to add the keyword synchronized before the method, and then the synchronized method can only have one thread at a time, and the other threads wait. The synchronous method is to use braces inside the method to make a block of code synchronized. The synchronization block has a synchronous "target" that makes the synchronization block more flexible (the synchronization block can determine which objects need to be locked through "target"). In general, if this "target" is this, then the synchronization method and the synchronization block are not much different.

In addition, it can be seen through anti-compilation that the synchronization block is two more instructions than the synchronous method. So the synchronization method is faster than the synchronization block.



The difference between non-static and Static is mainly (taking the synchronous method as an example): The non-static synchronization method is the lock class instance, and the static synchronization method is the lock class;

That is, for a non-static synchronization method, only one thread can enter a synchronous method at the same time in one instance of a class. However, for multiple instances, one thread of each instance can enter the same synchronous method.

Demo1: Multiple threads of an instance, only one thread at a time can enter a non-static synchronization method.

Package synchronizedtest;

/**
 * Created by Carrot on 16/8/31.
 */Public
class Syncfunc {public

    synchronized void Func1 () {
        System.out.println (Thread.CurrentThread (). GetName () + "is running");
        try {
            thread.sleep;
        } catch (Interruptedexception e) {
            e.printstacktrace ();
        }
        System.out.println (Thread.CurrentThread (). GetName () + "is Stop");
    }

    public static void Main (string[] args) {

        Newthread newThread1 = new Newthread ();
        Newthread newThread2 = new Newthread ();
        Newthread newThread3 = new Newthread ();

        Newthread1.start ();
        Newthread2.start ();
        Newthread3.start ();

    }
}

Class Newthread extends Thread {

    static syncfunc Syncfunc = new Syncfunc ();

    @Override public
    Void Run () {
        syncfunc.func1 ();
    }

}

Results:

Thread-0 is running
Thread-0 are stop
Thread-2 is running
Thread-2 are stop
Thread-1 is running
Thread-1 is stop

From the results, only one thread can enter a non-static synchronization method at a time.


Demo2: Multiple instances of a thread can simultaneously enter a non-static synchronization method.

Package synchronizedtest;

/**
 * Created by Carrot on 16/8/31.
 */Public
class Syncfunc {public

    synchronized void Func1 () {
        System.out.println (Thread.CurrentThread (). GetName () + "is running");
        try {
            thread.sleep;
        } catch (Interruptedexception e) {
            e.printstacktrace ();
        }
        System.out.println (Thread.CurrentThread (). GetName () + "is Stop");
    }

    public static void Main (string[] args) {

        Newthread newThread1 = new Newthread ();
        Newthread newThread2 = new Newthread ();
        Newthread newThread3 = new Newthread ();

        Newthread1.start ();
        Newthread2.start ();
        Newthread3.start ();

    }
}

Class Newthread extends Thread {

    Syncfunc syncfunc = new Syncfunc ();

    @Override public
    Void Run () {
        syncfunc.func1 ();
    }

}

Results:

Thread-0 is running
Thread-1 are running
Thread-2 is running
Thread-0 are stop
Thread-2 is stop
Thread-1 is stop

As you can see from the results, the threads of multiple instances enter the synchronous non-static method at the same time.


DEMO3: Threads of multiple instances Enter a static synchronization method.

Package synchronizedtest;

/**
 * Created by Carrot on 16/8/31.
 *
/public class Syncfunc {public

    static synchronized void Func1 () {
        System.out.println ( Thread.CurrentThread (). GetName () + "is running");
        try {
            thread.sleep;
        } catch (Interruptedexception e) {
            e.printstacktrace ();
        }
        System.out.println (Thread.CurrentThread (). GetName () + "is Stop");
    }

    public static void Main (string[] args) {

        Newthread newThread1 = new Newthread ();
        Newthread newThread2 = new Newthread ();
        Newthread newThread3 = new Newthread ();

        Newthread1.start ();
        Newthread2.start ();
        Newthread3.start ();

    }
}

Class Newthread extends Thread {

    Syncfunc syncfunc = new Syncfunc ();

    @Override public
    Void Run () {
        syncfunc.func1 ();
    }

}

Results:

Thread-0 is running
Thread-0 are stop
Thread-2 is running
Thread-2 are stop
Thread-1 is running
Thread-1 is stop

As can be seen from the results, for multiple instances of the same object, when entering a static synchronization method, only one instance of a class can enter at a time.


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.