|
1 | | -1. memo: |
2 | | -// 锁的元数据信息 |
3 | | -// 加锁过程的扩展点 |
4 | | -// 解锁过程的扩展点 |
5 | | -// 异常的扩展点 |
6 | | -// 数据源 |
| 1 | +# DistributedLock |
7 | 2 |
|
| 3 | +[](https://img.shields.io/maven-central/v/site.hellooo/hellooo-distributedlock) |
| 4 | +[](https://img.shields.io/github/license/hellooo-stack/hellooo-distributedlock) |
8 | 5 |
|
9 | | -2. features: |
| 6 | +DistributedLock is a lightweight distributed lock framework that provides reliable consistency features. It can be used with only the Lock interface. |
10 | 7 |
|
11 | 8 |
|
12 | | -3. implemented: |
| 9 | +# Features |
| 10 | +- Reentrant locking |
| 11 | +- Supports tryLock(), lock(), unlock() operations |
| 12 | +- Supports lock leasing |
13 | 13 |
|
14 | | -4. test schedule: |
| 14 | +# Quick Start |
| 15 | +Step one: Add maven dependency |
| 16 | +```xml |
| 17 | +<dependency> |
| 18 | + <groupId>site.hellooo</groupId> |
| 19 | + <artifactId>hellooo-distributedlock</artifactId> |
| 20 | + <version>>${hellooo-distributedlock.version}</version> |
| 21 | +</dependency> |
| 22 | +``` |
15 | 23 |
|
16 | | -5. what problems have I meet |
17 | | - - how to test private method: [sloved](https://stackoverflow.com/questions/34571/how-do-i-test-a-class-that-has-private-methods-fields-or-inner-classes) |
| 24 | +Step two: Add customize configuration by annotation |
| 25 | +```java |
| 26 | +public class Main { |
| 27 | + public static void main(String[] args) { |
| 28 | + LockOptions lockOptions = LockOptions.options() |
| 29 | + .build(); |
| 30 | + |
| 31 | +// define the redis source |
| 32 | + JedisPool pool = new JedisPool("localhost", 6379); |
| 33 | + for (int i = 0; i < 10; i++) { |
| 34 | + final int threadNumber = i; |
| 35 | + Thread thread = new Thread(() -> { |
| 36 | + Thread.currentThread().setName("Thread " + threadNumber); |
| 37 | + |
| 38 | + try (Jedis jedis = pool.getResource()) { |
| 39 | + try { |
| 40 | + Lock lock = new ReentrantDistributedLock(lockOptions, "my_lock", new RedisLockHandler(jedis)); |
| 41 | +// lock |
| 42 | + lock.lock(); |
| 43 | + System.out.println("thread" + Thread.currentThread().getName() + " locked!"); |
| 44 | + Thread.sleep(1000); |
| 45 | + System.out.println("thread" + Thread.currentThread().getName() + " lock released!"); |
| 46 | +// unlock |
| 47 | + lock.unlock(); |
| 48 | + } catch (Exception e) { |
| 49 | + e.printStackTrace(); |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | + thread.start(); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +``` |
0 commit comments