In-depth explanation of the differences between Synchronized and java. util. concurrent. locks. Lock

Source: Internet
Author: User

The main point is that Lock can complete all functions implemented by Synchronized.
Major difference: Lock has more precise threads than Synchronized and better performance.
Synchronized Automatically releases the Lock, but the Lock must be manually released by the programmer and must be released in the finally clause.
The synchronized modifier indicates that the same object is displayed as a synchronization queue in different threads.
If different objects are instantiated, synchronized will not have the synchronization effect.
1. Object lock
All objects automatically contain a single lock.
JVM is responsible for tracking the number of times an object is locked. If an object is unlocked, its count is 0. When the task (thread) locks the object for the first time, the Count changes to 1. Every time this same task (thread) gets a lock on this object, the Count increases progressively.
Only the task (thread) that first obtains the lock can continue to obtain multiple locks on the object.
Every time a task leaves a synchronized method, the Count decreases. When the count is 0, the lock is completely released, and other tasks can use this resource.
2. synchronized synchronization Block
2.1 synchronize to a single object lock
When a synchronization block is used, if all the synchronization blocks under the method are synchronized to the lock on an object, all tasks (threads) can only be mutually exclusive.
Resource1.javaDemonstrate how three threads (including the main thread) try to enter the synchronization blocks of three different methods of a class, although these synchronization blocks are in different methods, but because it is synchronized to the same object (the current object synchronized (this), their methods are still mutually exclusive.
For example
Copy codeThe Code is as follows: Class Test
{
Public static User user = null;
Public synchronized void add (User u)
{
User = u;
Dao. save (user)
}
}

If it is in thread 1Copy codeThe Code is as follows: Test test = new Test ();
User u = new User ();
U. setUserName ("liaomin ");
U. setUserPassword ("liaomin ");
Test. add (u );

In thread 2Copy codeThe Code is as follows: Test tes1t = new Test ();
User u1 = new User ();
U1.setUserName ("huqun ");
U1.setUserPassword ("huqun ");
Tes1t. add (u1 );

Now thread 1 and thread 2 start at the same time if the object new is not the same Test
If there is a thread crossover, the data inserted into the database will be the same.
Because when your user variable is static, you assign a value to it for the first time, if there is no save
When another thread changes the user value, the first thread inserts the value, that is, the value assigned for the second time.

So to achieve synchronization, you can change the method to static to achieve the synchronization effect.
Modify as follows:
Copy codeThe Code is as follows: Public static synchronized void add (User u)
{
User = u;
Dao. save (user)
}

The method to change to static exists in the heap.
The global method only exists for all instantiated and uninstantiated objects, so a synchronization queue will appear.
Of course, you can use lock without static.Copy codeThe Code is as follows: Class Test
{
Public static User user = null;
Lock lock = new ReentrantLock ();

Public void add (User u)
{
Lock. lock ();
User = u;
Dao. save (user );
Lock. unlock ();
}
}

In this way, no matter how many new objects you have, they will be synchronized by threads.
EquivalentCopy codeThe Code is as follows: Public static synchronized void add (User u)
{
User = u;
Dao. save (user)
}

At the same time, the lock performance is higher than synchronized.
You only need to manually disable lock.

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.