Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A tutorial on the method of implementing Redis distributed Lock from scratch by Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "Java from zero to achieve Redis distributed lock method tutorial", the explanation content in the article is simple and clear, easy to learn and understand, please follow the idea of Xiaobian slowly in-depth, together to study and learn "Java from zero to achieve Redis distributed lock method tutorial"!

Why distributed locks are needed

In jdk we have a way to lock:

1) synchronized keyword

(2) optimistic lock implemented by volatile + CAS

(3) ReadWriteLock

(4) ReenTrantLock reentrant lock

And so on, these locks become a great convenience for us to ensure thread safety in multithreaded situations.

But in distributed systems, the locks above are useless.

If we want to solve concurrency problems in distributed systems, we need to introduce the concept of distributed locks.

Java code to achieve creative motivation

First of all, it is a realization of lock realization principle, theory guides practice, practice perfects theory.

There were a lot of articles about redis distributed locks at night, but they were also mixed.

Redis distributed lock tool Sometimes middleware teams may not provide, provide and may not often maintain, it is better to implement one yourself, know the principle, but also easy to modify.

interface definition

To facilitate reuse with JDK, we let the interface inherit from jdk's Lock interface.

package com.github.houbb.lock.api.core; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; /** * Lock definition * @author binbin.hou * @since 0.0.1 */ public interface ILock extends Lock { /** * Try locking. * @param time * @param unit when * @param key key * @return Return * @throws InterruptedException * @since 0.0.1 */ boolean tryLock(long time, TimeUnit unit, String key) throws InterruptedException; /** * Try locking. * @param key key * @return Return * @since 0.0.1 */ boolean tryLock(String key); /** * unlock * @param key key * @since 0.0.1 */ void unlock(String key); }

Methods We've added only three core methods that are more commonly used as the first version, which is simpler.

Subsequent additions can be made one after another.

abstract implementation

In order to add more implementations later, a common abstract parent class is implemented first.

package com.github.houbb.lock.redis.core; import com.github.houbb.lock.api.core.ILock; import com.github.houbb.lock.redis.constant.LockRedisConst; import com.github.houbb.wait.api.IWait; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; /** * Abstract implementation * @author binbin.hou * @since 0.0.1 */ public abstract class AbstractLockRedis implements ILock { /** * lock wait * @since 0.0.1 */ private final IWait wait; protected AbstractLockRedis(IWait wait) { this.wait = wait; } @Override public void lock() { throw new UnsupportedOperationException(); } @Override public void lockInterruptibly() throws InterruptedException { throw new UnsupportedOperationException(); } @Override public boolean tryLock() { return tryLock(LockRedisConst.DEFAULT_KEY); } @Override public void unlock() { unlock(LockRedisConst.DEFAULT_KEY); } @Override public boolean tryLock(long time, TimeUnit unit, String key) throws InterruptedException { long startTimeMills = System.currentTimeMillis(); //One acquisition, direct success boolean result = this.tryLock(key); if(result) { return true; } //time judgment if(time

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report