Skip to content

Commit 529a876

Browse files
整合实现,提供线程上下文接口以达到线程级的锁参数控制
1 parent 7a3ff38 commit 529a876

26 files changed

Lines changed: 693 additions & 495 deletions

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ void main(String[] args) {
130130
**发行版中可以看到全部版本<br/>项目下的 jar 文件夹是当前最新的每夜版**
131131

132132
依赖的同系列项目
133-
- [PDConcurrent](https://github.com/fybug/PDUtilFunctionExpand)
133+
134+
- [PDUtilFunctionExpand](https://github.com/fybug/PDUtilFunctionExpand)
134135

135136
可通过 **WIKI** 或者 **doc文档** 深入学习本工具
136137

jar/PDConcurrent.jar

4.6 KB
Binary file not shown.

jar/PDConcurrent_all.jar

7.82 KB
Binary file not shown.

jar/PDConcurrent_bin.jar

4.6 KB
Binary file not shown.

jar/PDConcurrent_sources.jar

3.22 KB
Binary file not shown.

src/main/java/fybug/nulll/pdconcurrent/SyLock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* 使用 {@code new**Lock()} 的方法获取不同并发管理的实例<br/>
2121
*
2222
* @author fybug
23-
* @version 0.1.3
23+
* @version 0.1.4
2424
* @since PDConcurrent 0.0.1
2525
*/
2626
@SuppressWarnings("unused")
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package fybug.nulll.pdconcurrent.i;
2+
import java.util.function.Function;
3+
4+
import fybug.nulll.pdconcurrent.e.LockType;
5+
import fybug.nulll.pdutilfunctionexpand.tryFunction;
6+
import fybug.nulll.pdutilfunctionexpand.trySupplier;
7+
import jakarta.annotation.Nullable;
8+
import jakarta.validation.constraints.NotNull;
9+
10+
/**
11+
* <h2>读写锁管理基本框架.</h2>
12+
* 提供了读写锁实现整合接口
13+
*
14+
* @author fybug
15+
* @version 0.0.1
16+
* @since i 0.0.3
17+
*/
18+
@SuppressWarnings("unused")
19+
public abstract
20+
class AbstractRWSyLock<T> extends AbstractSyLock<T> {
21+
protected
22+
AbstractRWSyLock(@NotNull LockThreadContext<T> c) { super(c); }
23+
24+
/**
25+
* {@inheritDoc}
26+
*
27+
* @param lockType {@inheritDoc}
28+
* @param run {@inheritDoc}
29+
* @param catchby {@inheritDoc}
30+
* @param finaby {@inheritDoc}
31+
* @param <R> {@inheritDoc}
32+
* @param <E> {@inheritDoc}
33+
*
34+
* @return {@inheritDoc}
35+
*
36+
* @throws E1 {@inheritDoc}
37+
* @implNote 使用 {@link #lock(LockType)}、{@link #unlock()} 进行上锁解锁,因此使用两层的异常捕获避免{@code finaby}中抛出异常
38+
*/
39+
@SuppressWarnings("unchecked")
40+
@Override
41+
protected
42+
<R, E extends Throwable, E1 extends Throwable> R lockimpl(@NotNull LockType lockType, @NotNull trySupplier<R, E> run,
43+
@Nullable tryFunction<E, R, E1> catchby,
44+
@Nullable Function<R, R> finaby) throws E1
45+
{
46+
R o = null;
47+
// 防止finally内的回调抛异常
48+
try {
49+
try {
50+
// 上锁
51+
lock(lockType);
52+
// 主要内容
53+
o = run.get();
54+
} catch ( Throwable e ) {
55+
// 异常处理
56+
if ( catchby != null )
57+
o = catchby.apply((E) e);
58+
} finally {
59+
// 收尾
60+
if ( finaby != null )
61+
o = finaby.apply(o);
62+
}
63+
} finally {
64+
unlock();
65+
removeLockThreadContext();
66+
}
67+
return o;
68+
}
69+
70+
/**
71+
* {@inheritDoc}
72+
*
73+
* @param lockType {@inheritDoc}
74+
* @param run {@inheritDoc}
75+
* @param catchby {@inheritDoc}
76+
* @param finaby {@inheritDoc}
77+
* @param <R> {@inheritDoc}
78+
* @param <E> {@inheritDoc}
79+
*
80+
* @return {@inheritDoc}
81+
*
82+
* @throws E1 {@inheritDoc}
83+
* @implNote 使用 {@link #trylock(LockType)}、{@link #unlock()} 进行上锁解锁,因此使用两层的异常捕获避免{@code finaby}中抛出异常
84+
*/
85+
@SuppressWarnings("unchecked")
86+
@Override
87+
protected
88+
<R, E extends Throwable, E1 extends Throwable> R trylockimpl(@NotNull LockType lockType,
89+
@NotNull tryFunction<Boolean, R, E> run,
90+
@Nullable tryFunction<E, R, E1> catchby,
91+
@Nullable Function<R, R> finaby) throws E1
92+
{
93+
R o = null;
94+
// 防止finally内的回调抛异常
95+
try {
96+
try {
97+
// 上锁
98+
o = run.apply(trylock(lockType));
99+
} catch ( Throwable e ) {
100+
// 异常处理
101+
if ( catchby != null )
102+
o = catchby.apply((E) e);
103+
} finally {
104+
// 收尾
105+
if ( finaby != null )
106+
o = finaby.apply(o);
107+
}
108+
} finally {
109+
unlock();
110+
removeLockThreadContext();
111+
}
112+
return o;
113+
}
114+
115+
/**
116+
* 上锁函数
117+
* <p>
118+
* 按照设置的上下文参数要求可能会因为线程被中断而抛出{@link InterruptedException}异常
119+
*
120+
* @param lockType 锁类型
121+
*
122+
* @implSpec 在此处实现上锁功能,实现时应该支持根据上下文选择是否使用可以被线程中断的上锁方式
123+
*/
124+
protected abstract
125+
void lock(@NotNull LockType lockType);
126+
127+
/**
128+
* 尝试上锁函数
129+
*
130+
* @param lockType 锁类型
131+
*
132+
* @return 是否成功上锁
133+
*
134+
* @implSpec 在此处实现尝试上锁功能,实现时应该支持上下文参数
135+
*/
136+
protected abstract
137+
boolean trylock(@NotNull LockType lockType);
138+
139+
/**
140+
* 解锁函数
141+
*
142+
* @implSpec 在此处实现解锁功能,解锁的类型按照最后上锁的类型处理
143+
*/
144+
public abstract
145+
void unlock();
146+
147+
/**
148+
* 检查锁是否被占用
149+
*
150+
* @return 是否被占用
151+
*/
152+
public abstract
153+
boolean isLocked();
154+
155+
/**
156+
* 检查当前线程是否持有锁
157+
*
158+
* @return 当前线程是否持有锁
159+
*/
160+
public abstract
161+
boolean isLockedCurrentThread();
162+
}

0 commit comments

Comments
 (0)