Distributed lock----Zookeeper practice (exclusive lock)

Source: Internet
Author: User

Exclusive Lock Concept:

Exclusive Locks, known as X-Lock, write-lock, exclusive-lock. If a thing T1 a data object O1 with exclusive locks, only the transaction T1 read and write to the O1 during the entire lock-up, and other transactions must wait until the T1 releases the lock before the operation can be performed. In a stand-alone environment, The JDK provides the Synchronized keyword and reentrantlock

The ability to reuse locks to provide exclusive locks.

Zookeeper implementation of the exclusive lock principle:

When you need to obtain an exclusive lock, all clients call the Create method under the fixed path of creating the node, in the concurrent environment, only one client can create a success, the equivalent of acquiring a lock, when the client completes the transaction, the node is deleted. For other nodes that did not succeed, set the listener on that path. If the path child node deletes an event trigger, continue to try to acquire the lock (create).

Specific implementation:

 

Define the interface of the Lock: (Several methods I want to implement)

 Public Interface Distributedlock {    void  lock ();     Boolean isLocked ();     Boolean Trylock ();     boolean trylock (long  timeout);     void unlock ();}

Distributed lock----Zookeeper practice (exclusive 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.