Skip to content

Commit 4431155

Browse files
committed
version 0.6.0.0 update
完善读写锁,现在可以自定义公平非公平
1 parent ba558ca commit 4431155

9 files changed

Lines changed: 46 additions & 11 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,7 @@
6464
<tr>
6565
<td>0.5.0.0</td><td>优化执行逻辑,提升互斥锁性能</td><td>2021年1月31日</td>
6666
</tr>
67+
<tr>
68+
<td>0.6.0.0</td><td>完善读写锁,现在可以自定义公平非公平</td><td>2022年2月2日</td>
69+
</tr>
6770
</table>

src/main/java/org/springframework/lock/annotation/ReadLock.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@
99
@Target(ElementType.METHOD)
1010
@Documented
1111
public @interface ReadLock {
12+
13+
/**
14+
* 是否是公平锁
15+
* @return 默认非公平
16+
*/
17+
boolean fair() default false;
1218
}

src/main/java/org/springframework/lock/annotation/WriteLock.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@
99
@Target(ElementType.METHOD)
1010
@Documented
1111
public @interface WriteLock {
12+
13+
/**
14+
* 是否是公平锁
15+
* @return 默认非公平锁
16+
*/
17+
boolean fair() default false;
1218
}

src/main/java/org/springframework/lock/annotation/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
* 每个注解上都有对应的注释,写得很清楚,自己看就可以了,不加以赘述.
44
* 需要注意的是,如果需要使锁注解生效,需要在springboot启动类上添加{@code @EnableSpringLocks}注解
55
* @author 宗祥瑞
6-
* @version 0.5.0.0
6+
* @version 0.6.0.0
77
*/
88
package org.springframework.lock.annotation;

src/main/java/org/springframework/lock/aspect/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
* 每个切面都有注释,没什么好说的,不加以赘述.
44
* 切面这部分需要托管到spring容器中去执行.
55
* @author 宗祥瑞
6-
* @version 0.5.0.0
6+
* @version 0.6.0.0
77
*/
88
package org.springframework.lock.aspect;

src/main/java/org/springframework/lock/processor/ReadLockProcessor.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@
1010
import com.sun.tools.javac.tree.TreeMaker;
1111
import com.sun.tools.javac.tree.TreeTranslator;
1212
import com.sun.tools.javac.util.*;
13+
import org.springframework.lock.annotation.ReadLock;
1314

1415
import javax.annotation.processing.*;
1516
import javax.lang.model.SourceVersion;
1617
import javax.lang.model.element.Element;
1718
import javax.lang.model.element.ExecutableElement;
1819
import javax.lang.model.element.TypeElement;
1920
import javax.tools.Diagnostic;
21+
import java.util.HashMap;
2022
import java.util.HashSet;
23+
import java.util.Map;
2124
import java.util.Set;
2225

2326
import static com.sun.tools.javac.tree.JCTree.*;
2427
import static com.sun.tools.javac.util.List.nil;
28+
import static com.sun.tools.javac.util.List.of;
2529

2630
/**
2731
* 读锁的处理器,用来将读锁编译进类的成员变量
@@ -76,10 +80,16 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
7680
for (TypeElement annotation : annotations) {
7781
Set<TypeElement> cls = new HashSet<TypeElement>();
7882
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(annotation);
83+
Map<TypeElement, Map<String, Object>> map = new HashMap<>();
7984
// 找到都有哪些类里面的方法用到了这个注解
8085
for (Element element : elements) {
8186
ExecutableElement method = (ExecutableElement) element;
8287
TypeElement clz = (TypeElement) method.getEnclosingElement();
88+
Map<String, Object> properties = new HashMap<>();
89+
ReadLock anno = method.getAnnotation(ReadLock.class);
90+
properties.put("fair", anno.fair());
91+
if (!map.containsKey(clz))
92+
map.put(clz, properties);
8393
messager.printMessage(Diagnostic.Kind.NOTE, "发现需要包含注解" + annotation.getQualifiedName() + "的类" + clz.getQualifiedName());
8494
cls.add(clz);
8595
}
@@ -111,7 +121,7 @@ public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
111121
// 修改语法树
112122
if (!foundReadWriteLock) {
113123
messager.printMessage(Diagnostic.Kind.NOTE, "将为类" + clz.getQualifiedName() + "动态生成读写锁");
114-
JCVariableDecl lock = makeReadWriteLock(clz);
124+
JCVariableDecl lock = makeReadWriteLock(clz, map.get(clz));
115125
jcClassDecl.defs = jcClassDecl.defs.append(lock);
116126
}
117127
if (!foundReadLock) {
@@ -133,7 +143,7 @@ public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
133143
* @param clz 要添加锁的类
134144
* @return 变量声明
135145
*/
136-
private JCVariableDecl makeReadWriteLock(TypeElement clz){
146+
private JCVariableDecl makeReadWriteLock(TypeElement clz, Map<String, Object> properties) {
137147
// 导入包
138148
JCCompilationUnit imports = (JCCompilationUnit) this.javacTrees.getPath(clz).getCompilationUnit();
139149
imports.defs = imports.defs.append(this.treeMaker.Import(this.treeMaker.Select(this.treeMaker.Ident(names.fromString("java.util.concurrent.locks")), this.names.fromString("ReentrantReadWriteLock")), false));
@@ -143,7 +153,7 @@ private JCVariableDecl makeReadWriteLock(TypeElement clz){
143153
modifiers,
144154
this.names.fromString("$lock"),
145155
this.memberAccess("java.util.concurrent.locks.ReentrantReadWriteLock"),
146-
this.treeMaker.NewClass(null, nil(), treeMaker.Ident(names.fromString("ReentrantReadWriteLock")), nil(), null)
156+
this.treeMaker.NewClass(null, of(memberAccess("java.lang.Boolean")), treeMaker.Ident(names.fromString("ReentrantReadWriteLock")), of(this.treeMaker.Literal(properties.get("fair"))), null)
147157
);
148158
return var;
149159
}

src/main/java/org/springframework/lock/processor/WriteLockProcessor.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@
1010
import com.sun.tools.javac.tree.TreeTranslator;
1111
import com.sun.tools.javac.util.Context;
1212
import com.sun.tools.javac.util.Names;
13+
import org.springframework.lock.annotation.WriteLock;
1314

1415
import javax.annotation.processing.*;
1516
import javax.lang.model.SourceVersion;
1617
import javax.lang.model.element.Element;
1718
import javax.lang.model.element.ExecutableElement;
1819
import javax.lang.model.element.TypeElement;
1920
import javax.tools.Diagnostic;
21+
import java.util.HashMap;
2022
import java.util.HashSet;
23+
import java.util.Map;
2124
import java.util.Set;
2225

2326
import static com.sun.tools.javac.util.List.nil;
2427
import static com.sun.tools.javac.tree.JCTree.*;
28+
import static com.sun.tools.javac.util.List.of;
2529

2630
/**
2731
* 写锁的处理器,用来将写锁编译进类的成员变量
@@ -76,10 +80,16 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
7680
for (TypeElement annotation : annotations) {
7781
Set<TypeElement> cls = new HashSet<TypeElement>();
7882
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(annotation);
83+
Map<TypeElement, Map<String, Object>> map = new HashMap<>();
7984
// 找到都有哪些类里面的方法用到了这个注解
8085
for (Element element : elements) {
8186
ExecutableElement method = (ExecutableElement) element;
8287
TypeElement clz = (TypeElement) method.getEnclosingElement();
88+
Map<String, Object> properties = new HashMap<>();
89+
WriteLock anno = method.getAnnotation(WriteLock.class);
90+
properties.put("fair", anno.fair());
91+
if (!map.containsKey(clz))
92+
map.put(clz, properties);
8393
messager.printMessage(Diagnostic.Kind.NOTE, "发现需要包含注解" + annotation.getQualifiedName() + "的类" + clz.getQualifiedName());
8494
cls.add(clz);
8595
}
@@ -111,7 +121,7 @@ public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
111121
// 修改语法树
112122
if (!foundReadWriteLock) {
113123
messager.printMessage(Diagnostic.Kind.NOTE, "将为类" + clz.getQualifiedName() + "动态生成读写锁");
114-
JCVariableDecl lock = makeReadWriteLock(clz);
124+
JCVariableDecl lock = makeReadWriteLock(clz, map.get(clz));
115125
jcClassDecl.defs = jcClassDecl.defs.append(lock);
116126
}
117127
if (!foundWriteLock) {
@@ -133,7 +143,7 @@ public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
133143
* @param clz 要添加锁的类
134144
* @return 读写锁变量声明
135145
*/
136-
private JCTree.JCVariableDecl makeReadWriteLock(TypeElement clz){
146+
private JCTree.JCVariableDecl makeReadWriteLock(TypeElement clz, Map<String, Object> properties){
137147
// 导入包
138148
JCCompilationUnit imports = (JCCompilationUnit) this.javacTrees.getPath(clz).getCompilationUnit();
139149
imports.defs = imports.defs.append(this.treeMaker.Import(this.treeMaker.Select(this.treeMaker.Ident(names.fromString("java.util.concurrent.locks")), this.names.fromString("ReentrantReadWriteLock")), false));
@@ -143,7 +153,7 @@ private JCTree.JCVariableDecl makeReadWriteLock(TypeElement clz){
143153
modifiers,
144154
this.names.fromString("$lock"),
145155
this.memberAccess("java.util.concurrent.locks.ReentrantReadWriteLock"),
146-
this.treeMaker.NewClass(null, nil(), treeMaker.Ident(names.fromString("ReentrantReadWriteLock")), nil(), null)
156+
this.treeMaker.NewClass(null, of(memberAccess("java.lang.Boolean")), treeMaker.Ident(names.fromString("ReentrantReadWriteLock")), of(this.treeMaker.Literal(properties.get("fair"))), null)
147157
);
148158
return var;
149159
}

src/main/java/org/springframework/lock/processor/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
* 它的原理与lombok有些许类似,但代码风格比lombok更好理解,一看就懂
55
* 每个处理器都写了注释,写得都很清楚,不加以赘述
66
* @author 宗祥瑞
7-
* @version 0.5.0.0
7+
* @version 0.6.0.0
88
*/
99
package org.springframework.lock.processor;

src/test/java/example/name/service/BaseService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public String testSynchronized() {
3333
return "testSynchronized 执行结束";
3434
}
3535

36-
@ReadLock
36+
@ReadLock(fair = true)
3737
public String testReadLock() {
3838
String name = Thread.currentThread().getName();
3939
LOGGER.info(name + "开始执行");
@@ -46,7 +46,7 @@ public String testReadLock() {
4646
return "testReadLock 执行结束";
4747
}
4848

49-
@WriteLock
49+
@WriteLock(fair = true)
5050
public String testWriteLock(){
5151
String name = Thread.currentThread().getName();
5252
LOGGER.info(name + "开始执行");

0 commit comments

Comments
 (0)