Java Thread synchroize The pros and cons of two different ways

Source: Internet
Author: User

Context:

About thread always read the forget, see forget. Let's write it down. Translation is the Orcale official website of the lock explaining the document address


Lock:

The synchronization mechanism revolves around the internal entity, intrinsic lock (intrinsic intrinsic, intrinsic), monitor lock (monitoring lock). Intrinsic lock plays two roles in sync: 1. Forced exclusivity reads the state of the object (that is, the thread accesses the object's properties, methods, etc.), and 2 remains happens-before relationship to ensure the visibility of the variable and method.

Each object has a intrinsic lock associated with it. By convention, a thread requires an exclusive, consistent (consistent) read of an object's properties and must obtain a lock on that object. and release intrinsic lock after the process is finished. Between thread fetch and release locks, threads are known to have intrinsic lock. As long as a thread holds intrinsic lock, other threads are blocked when they try to acquire the lock.


Two kinds of synchronized:Synchronized method:

Usage: public synchronized void SayHello () {}

A) non-static method: Intrinsic lock is the object of the calling method, such as intrinsic lock that the B.sayhello thread holds is the intrinsic lock of object B

b) Static method: For example public static synchronized void getinstance (). The call is Test.getinstance (), there is no object, the thread holds the intrinsic lock is the object of the class class of the test object, which is the object that is assumed to be obtained by the object Testobject.getclass () of the Test class. The object is to generate a Java.lang.Class object in the Java heap that represents the class in the load (Loading) phase of the class as the access entry for the method area. (See my previous blog Click to open the link ) so he is a class-level lock.

Synchronized statement:

Usage:

public void AddName (String name) {    synchronized (this) {        lastName = name;        namecount++;    }    Namelist.add (name);}
A) Instead of using the method's object as intrinsic lock, you specify any object

Benefits

1: Instead of synchronizing the entire method, it is more efficient and more secure to synchronize the parts (the method of invoking other objects from a synchronized method can cause liveness to ask (e.g. deadlock). As an example, there is no namelist.add (name); add it to the inside.

:

public class Mslunch {    private long c1 = 0;    Private long C2 = 0;    Private Object lock1 = new Object ();    Private Object Lock2 = new Object ();    public void Inc1 () {        synchronized (lock1) {            c1++;        }    }    public void Inc2 () {        synchronized (lock2) {            c2++;}}    }
There is no logical correlation between changes in 2.c1 and C2, and two threads can cross-change. So there is no need to use synchronized method, otherwise the lock is the object, two inc1, INC2 can not be accessed by two threads simultaneously.


Re-entry synchronized:

Although the a thread cannot enter the method that the B thread locks, the B thread can again enter the method of the B thread holding the lock and accumulate the lock entry. So you can call it recursively.


Java Thread synchroize The pros and cons of two different ways

Related Article

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.