Distributed lock-segment lock based on memcached Redis Zookeeper (3 resource modes) implementation

Source: Internet
Author: User
Tags memcached redis zookeeper
Example
Add a lock to segmentation_object this string
objectlock Objectsegmentationlock = new Objectlock ("Segmentation_object");

try {

    objectsegmentationlock.lock ();
    DoSomething ();

} catch (Exception e) {
    e.printstacktrace ();
} finally {

    objectsegmentationlock.releaselock ();
}


try {
    //attempt to lock up until 5 seconds to timeout lock failure
    if (Objectsegmentationlock.trylock (5000)) {
        dosomething ()}
    }

catch (Exception e) {
    e.printstacktrace ();
} finally {

    objectsegmentationlock.releaselock ();
}


Codelock Codelock = new Codelock ();

try {
    //directly block and lock the following code (dosomething) until the lock
    Codelock.lock () is obtained;
    DoSomething ();

} catch (Exception e) {
    e.printstacktrace ();
} finally {

    codelock.releaselock ();
}

try {
    //directly attempt to lock the following code (dosomething) until 5 seconds after timeout lock failure
    if (Codelock.trylock (5000)) {
        dosomething ();
    }

} catch (Exception e) {
    e.printstacktrace ();
} finally {

    codelock.releaselock ();
}
Source Code

Https://github.com/lichao4Java/distributed-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.