The difference between Java concurrency Lock and synchronized

Source: Internet
Author: User
Tags finally block

1 lock is an interface, while synchronized is the key word in Java, synchronized is the built-in language implementation;

2) synchronized in the event of an exception, will automatically release the thread-occupied locks, so that the deadlock does not occur, and lock in the event of an exception, if not active through the unlock () to release the lock, it is likely to cause a deadlock phenomenon, Therefore, lock should be released in finally block when using lock;

3 lock can let the thread waiting for the lock to respond to interrupt, and synchronized but not, when using synchronized, the waiting thread will wait, can not respond to interrupts;

4 through lock can know whether there is a successful acquisition of locks, and synchronized is unable to do.

5 lock can improve the efficiency of multiple threads for read operations.

In terms of performance, if the competitive resources are not fierce, the performance of the two is similar, and when the competitive resources are very intense (that is, a large number of threads at the same time competition), the lock performance is far superior to synchronized. Therefore, in the specific use of the appropriate circumstances to choose.

The two differ in the related concept of the lock:

1. Can be re-entry lock

If the lock is reentrant, it is said to be a reentrant lock. Like synchronized and Reentrantlock are reentrant locks, and reentrant, in my opinion, actually shows the allocation mechanism for locks: thread based allocations, not allocations based on method calls. As a simple example, when a thread executes to a synchronized method, such as Method1, and another synchronized method is invoked in Method1, the thread does not have to reapply for the lock. Instead, you can execute the method method2 directly.

Look at the code below to see:

1 2 3 4 5 6 7 8 9 Class MyClass {public synchronized void Method1 () {method2 (); Public synchronized void Method2 () {}}

The two methods in the above code, METHOD1 and Method2, are decorated with synchronized, and if at one point thread a executes to the METHOD1, thread A acquires the lock of the object, and since METHOD2 is also the Synchronized method, If synchronized is not available, thread A will need to reapply for the lock at this point. But this creates a problem because thread a already holds the lock on the object and is requesting a lock on the object, so that thread a waits for a lock that will never be acquired.

And because synchronized and lock have the ability to be reentrant, so this phenomenon will not occur.

2. Can break lock

Interruptible Lock: As the name suggests, is the lock that can be interrupted accordingly.

In Java, synchronized is not an interruptible lock, and lock is an interruptible lock.

If a thread A is executing the code in the lock, another thread B is waiting to acquire the lock, possibly because the wait time is too long, threads B does not want to wait, wants to deal with other things first, we can let it interrupt itself or interrupt it in another thread, this is an interruptible lock.

The use of lockinterruptibly () is demonstrated in the preceding example to show the interruptible nature of lock.

3. Fair lock

A fair lock is as much as possible to obtain a lock in the order in which it is requested. For example, there are multiple threads waiting for a lock, and when the lock is released, the longest-waiting thread (the first requested thread) gets it, which is a fair lock.

Non-fair locks do not guarantee that locks are obtained in the order in which they are requested. This can cause one or some threads to never acquire a lock.

In Java, synchronized is an unjust lock that does not guarantee that the waiting thread gets the order of the locks.

For Reentrantlock and Reentrantreadwritelock, it is not a fair lock by default, but it can be set to a fair lock.

A look at the source code of these 2 classes is clear:

  

In Reentrantlock, 2 static inner classes are defined, one is Notfairsync and the other is Fairsync, which is used to implement unfair locks and fair locks, respectively.

We can set the fairness of the lock in the following ways when creating the Reentrantlock object:

1 Reentrantlock lock = new Reentrantlock (true);

If the argument is true, it is a fair lock, and Fasle is a non fair lock. By default, if you use an parameterless constructor, it is a fair lock.

  

In addition, many methods are defined in the Reentrantlock class, such as:

Isfair ()//Determine if the lock is a fair lock

IsLocked ()//To determine if the lock was acquired by any thread

Isheldbycurrentthread ()//To determine if the lock was acquired by the current thread

Hasqueuedthreads ()//judge whether the thread is waiting for the lock

Similar methods are also available in Reentrantreadwritelock, which can also be set to fair and unjust locks. Remember, however, that Reentrantreadwritelock does not implement the lock interface, it implements the Readwritelock interface.

4. Read and Write lock

A read-write lock divides access to a resource (such as a file) into 2 locks, a read lock, and a write lock.

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.