And look back on the 17th day of Java

Source: Internet
Author: User

Sync Lock
After JDk1.5, Java provides another thread synchronization mechanism: it implements thread synchronization by explicitly defining a synchronization lock object, in which case the synchronization lock should use the lock object to act.
It is generally thought that lock provides a wider range of operations than the Synchronized method and synchronized code blocks, the lock implementation allows for a more flexible structure, can have very different properties, and can support multiple related condition objects
Lock is a tool that controls the access of multiple threads to shared resources. Normally lock provides exclusive access to a shared resource, allowing only one thread to lock the lock object at a time, and the thread should obtain the lock object before accessing the shared resource, although some lock may allow concurrent access to the shared resource, such as Readwritelock (read-write lock), Of course, in implementing thread-safe control, it is common to prefer to use the Reentrantlock (reentrant Lock) code format, which typically uses lock:
Class x{
Defining Lock objects
Private final Reentrantlock lock=new Reentrantlock ();
Methods that require a guaranteed thread
public void M () {
Lock.lock ();
try{
Code blocks to ensure thread safety
.....
}finally{
Lock.unlock ()
}
}
Using the lock object for synchronization is when lock and release locks appear in different scopes, it is generally recommended to use the finally block to ensure that the lock is released when necessary
Using lock is a bit similar to using the synchronous method, except that using lock is explicitly specifying the lock object as a synchronous lock, while using the synchronous method implicitly uses the current object as the synchronization monitor
Synchronous methods and synchronous code blocks use implicit synchronization monitors that are related to competing resources, and enforce lock and release locks to appear in the same block structure, and when multiple locks are acquired, they must be released in reverse order, and the lock must be released within the same range as all locks are fetched.
Lock provides functionality that is not available for synchronous methods and synchronous code blocks, including the Trylock method for non-block structures, and the lockinterruptibly () method that attempts to obtain an interruptible lock, and the Trylock (Long,timeunit) method for obtaining a timeout failure.
Reentrantlock locks have re-entry, that is, the thread can lock the locked Reentrantlock lock again, Reentrantlock will maintain a counter to track nested calls to the lock method, the thread after each lock () locks, You must explicitly call unlock () to release the lock, and the lock-protected code can invoke another method that is protected by the same lock

And look back on the 17th day of Java

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.