Skip to content
Distributed lock sample
Java
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
LICENSE
README.md
build.gradle
build.json

README.md

distributed-lock-sample

Distributed lock using memcached/redis.


Install memcached on macOS:

$ brew install memcached

Run memcached:

$ memcached [-p <PORT>]

Code sample:

MemcachedClient client = new XMemcachedClient("127.0.0.1", 11211);
DistributedLockFactory factory = new XMemcachedDistributedLockFactory(client);

DistributedLock distributedLock = factory.createDistributedLock("a");
if (distributedLock.tryLock()) {
    System.out.println("LOCK SUCCESS IN :" + System.currentTimeMillis());
    try {
        ThreadUtil.sleep(10L);
    } finally {
        System.out.println("LOCK SUCCESS OUT:" + System.currentTimeMillis());
        distributedLock.unLock();
    }
} else {
    System.out.println("LOCK FAIL:" + System.currentTimeMillis());
}

Libraries:

You can’t perform that action at this time.