main similarities: Lock is capable of accomplishing all the functions implemented by synchronized.
Main differences: Lock has more precise threading than synchronized and better performance. the synchronized will automatically release the lock, but lock must require the programmer to release it manually and must be released in the finally clause.
Synchronized a modified method indicates that the same object behaves as a synchronous queue in a different thread
If you instantiate a different object then synchronized will not have synchronization effect.
1. Lock of the object
All objects automatically contain a single lock.
The JVM is responsible for tracking the number of times an object is locked. If an object is unlocked, its count becomes 0. The count becomes 1 when the task (thread) locks the object for the first time. Each time the same task (thread) obtains a lock on this object, the count is incremented.
Only the task (thread) that acquires the lock first can continue to acquire multiple locks on the object.
Whenever a task leaves a synchronized method, the Count decrements, and when the count is 0, the lock is completely freed, and the resource is available for other tasks at this time.
2.synchronized Sync block
2.1 Synchronizing to a single object lock
When synchronizing blocks are used, if the synchronization blocks under the method are synchronized to a lock on an object, then all the tasks (threads) can only be mutually exclusive into these synchronization blocks.
Resource1.java demonstrates that three threads (including the main thread) attempt to enter a synchronization block of three different methods of a class, although these synchronization blocks are in different methods, but because they are synchronized to the same object (the current object synchronized ( This), so the approach to them is still mutually exclusive.
Like what
Class test{public static User User=null; Public synchronized void Add (User u) { user=u; Dao.save (user) }}
If you are in thread 1
Test test=new test (); User U=new User (); U.setusername ("liaomin"); U.setuserpassword ("liaomin"); Test.add (U);
If you are in thread 2
Test tes1t=new test (); User U1=new User (); U1.setusername ("Huqun"); U1.setuserpassword ("Huqun"); Tes1t.add (U1);
So now thread 1 and thread 2 start at the same time if the object new is not the same test
Then the threads intersect then the data in the inserted database is the same
Because your user variable is static, you assign it to him for the first time.
Another thread changed the value of user so the first thread was inserted and the second value was given.
So to achieve synchronization then you can change the method to static can achieve the effect of synchronization
public static synchronized void Add (User u) { user=u; Dao.save (User)}
The method modified to static is present in the heap
is a global method needle there is only one for all instantiated and non-instantiated objects, so the synchronization queue appears
Of course, no static, but you can use lock.
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 (); }}
So no matter how many objects you're new, it's thread-synchronized.
Equivalent
public static synchronized void Add (User u) { user=u; Dao.save (User)}
At the same time, lock performance is higher than synchronized
Just lock needs to be closed manually
The difference between synchronized and Java.util.concurrent.locks.Lock is detailed