Java--redis Concurrent Read and write locks, using Redisson to implement distributed locks __redis

Source: Internet
Author: User
Tags semaphore

Reprint please indicate the source: http://blog.csdn.net/l1028386804/article/details/73523810

Recently because the work is very busy, very long time did not update the blog, today for everyone to bring an article on the implementation of distributed lock Redisson, well, not much said, directly into the subject. 1. Reentrant Lock (reentrant lock)

The Redisson distributed reentrant lock Rlock Java object implements the Java.util.concurrent.locks.Lock interface and also supports automatic expiration of the unlock.

public void Testreentrantlock (Redissonclient redisson) {
	Rlock lock = Redisson.getlock ("AnyLock");
	try{
		//1. The most common use method
		//lock.lock ();
		2. Support the expiration of the unlock function, 10 seconds after the automatic unlock, without invoking the Unlock method manually unlock
		//lock.lock (ten, timeunit.seconds);
		3. Try locking, wait up to 3 seconds, lock after 10 seconds automatically unlock
		boolean res = Lock.trylock (3, timeunit.seconds);
		if (res) {//Success
		//Do your business
		}
	catch (Interruptedexception e) {
		e.printstacktrace ();
	} finally {
		lock.unlock ();
	}
}
Redisson also provides methods for asynchronous execution of distributed locks:
public void Testasyncreentrantlock (Redissonclient redisson) {
	Rlock lock = Redisson.getlock ("AnyLock");
	try{
		Lock.lockasync ();
		Lock.lockasync (timeunit.seconds);
		future<boolean> res = Lock.trylockasync (3, timeunit.seconds);
		if (Res.get ()) {
		//Do your business
		}
	catch (Interruptedexception e) {
		e.printstacktrace ();
	} catch (Executionexception e) {
		e.printstacktrace ();
	} finally {
		lock.unlock ();
	}
}

2. Fair Lock (Fair Lock)

Redisson Distributed Reentrant Fair Lock is also a Rlock object to implement the Java.util.concurrent.locks.Lock interface. When the automatic expiration unlocking function is provided, it is ensured that when multiple Redisson client threads request lock at the same time, priority is assigned to the thread that first makes the request.

public void Testfairlock (Redissonclient redisson) {
	Rlock fairlock = Redisson.getfairlock ("AnyLock");
	try{
		//The most common use method
		Fairlock.lock ();
		Support the expiration of the unlock function, 10 seconds after the automatic unlock, without invoking the Unlock method manually unlock
		Fairlock.lock (ten, timeunit.seconds);
		Try locking, wait up to 100 seconds, lock after 10 seconds automatically unlock
		boolean res = Fairlock.trylock (M, timeunit.seconds);
	Interruptedexception e) {
		e.printstacktrace ();
	} finally {
		fairlock.unlock ();
	}
}
Redisson also provides a way to perform asynchronous execution for distributed, reentrant, fair locks:
Rlock Fairlock = Redisson.getfairlock ("AnyLock");
Fairlock.lockasync ();
Fairlock.lockasync (timeunit.seconds);
future<boolean> res = Fairlock.trylockasync (M, timeunit.seconds);

3. Interlock (Multilock)

Redisson Redissonmultilock objects can associate multiple Rlock objects as one interlock, and each Rlock object instance can come from a different Redisson instance.

public void Testmultilock (redissonclient redisson1,redissonclient redisson2, redissonclient redisson3) {
	Rlock Lock1 = Redisson1.getlock ("Lock1");
	Rlock Lock2 = Redisson2.getlock ("Lock2");
	Rlock Lock3 = Redisson3.getlock ("Lock3");
	Redissonmultilock lock = new Redissonmultilock (Lock1, Lock2, lock3);
	try {
		//simultaneous lock: Lock1 lock2 lock3, all locks are locked successfully.
		Lock.lock ();
		Try locking, wait up to 100 seconds, lock after 10 seconds automatically unlock
		boolean res = Lock.trylock (M, timeunit.seconds);
	Interruptedexception e) {
		e.printstacktrace ();
	} finally {
		lock.unlock ();
	}
}

4. Red Lock (Redlock)

The Redisson Redissonredlock object realizes the lock algorithm introduced by Redlock. The object can also be used to associate multiple Rlock objects as a red lock, and each Rlock object instance can come from a different Redisson instance.

public void Testredlock (redissonclient redisson1,redissonclient redisson2, redissonclient redisson3) {
	Rlock lock1 = Redisson1.getlock ("Lock1");
	Rlock Lock2 = Redisson2.getlock ("Lock2");
	Rlock Lock3 = Redisson3.getlock ("Lock3");
	Redissonredlock lock = new Redissonredlock (Lock1, Lock2, lock3);
	try {
		//At the same time lock: Lock1 lock2 Lock3, Red lock on most nodes lock success even if successful.
		Lock.lock ();
		Try locking, wait up to 100 seconds, lock after 10 seconds automatically unlock
		boolean res = Lock.trylock (M, timeunit.seconds);
	Interruptedexception e) {
		e.printstacktrace ();
	} finally {
		lock.unlock ();
	}
}

5. Read and write Lock (Readwritelock)

The Redisson distributed reentrant read-write lock Rreadwritelock,java object implements the Java.util.concurrent.locks.ReadWriteLock interface. It also supports automatic expiration of the unlock. This object allows multiple read locks at the same time, but only one write lock is allowed.

Rreadwritelock Rwlock = Redisson.getlock ("Anyrwlock");
The most common use method is
rwlock.readlock (). Lock ();
or
Rwlock.writelock (). Lock ();
Support for expired unlock function
//10 seconds after automatic unlock
//without invoking the Unlock method manually unlock
rwlock.readlock (). Lock (timeunit.seconds);
or
Rwlock.writelock (). Lock (timeunit.seconds);
Try locking for up to 100 seconds, lock after 10 seconds automatically unlock
boolean res = Rwlock.readlock (). Trylock (S., timeunit.seconds);
or
Boolean res = Rwlock.writelock (). Trylock (timeunit.seconds);
...
Lock.unlock ();

6. Signal Volume (semaphore)

The Redisson distributed Semaphore (semaphore) Java object Rsemaphore uses interfaces and usages similar to Java.util.concurrent.Semaphore.

Rsemaphore semaphore = Redisson.getsemaphore ("semaphore");
Semaphore.acquire ();
or
Semaphore.acquireasync ();
Semaphore.acquire (a);
Semaphore.tryacquire ();
or
Semaphore.tryacquireasync ();
Semaphore.tryacquire (timeunit.seconds);
or
Semaphore.tryacquireasync (timeunit.seconds);
Semaphore.release (ten);
Semaphore.release ();
or
semaphore.releaseasync ();

7. Expiration signal Amount (Permitexpirablesemaphore)

The Redisson semaphore (Permitexpirablesemaphore) is Rsemaphore object based, adding an expiration time for each signal. Each signal can be identified by a separate ID, which can only be released by submitting the ID.

Rpermitexpirablesemaphore semaphore = Redisson.getpermitexpirablesemaphore ("Mysemaphore");
String Permitid = Semaphore.acquire ();
Gets a signal that is valid for only 2 seconds.
String Permitid = Semaphore.acquire (2, timeunit.seconds);
// ...
Semaphore.release (Permitid);

8. Atresia (Countdownlatch)

The Redisson distributed latching (Countdownlatch) Java object Rcountdownlatch employs interfaces and usages similar to Java.util.concurrent.CountDownLatch.

Rcountdownlatch latch = Redisson.getcountdownlatch ("Anycountdownlatch");
Latch.trysetcount (1);
Latch.await ();
Rcountdownlatch latch = Redisson.getcountdownlatch ("Anycountdownlatch") in other threads or other JVMs;
Latch.countdown ();


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.