Skip to content

Commit bf17330

Browse files
committed
add springboot-SpringFactoriesLoader.png
1 parent edaf073 commit bf17330

File tree

5 files changed

+156
-7
lines changed

5 files changed

+156
-7
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.learnjava.concurent;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
5+
import java.util.concurrent.locks.Condition;
6+
import java.util.concurrent.locks.Lock;
7+
8+
/**
9+
*
10+
* 同时有两个线程获取锁
11+
*
12+
* @author LuoHaiYang
13+
*/
14+
public class TwinsLock implements Lock {
15+
16+
private final Sync sync = new Sync(2);
17+
18+
private static final class Sync extends AbstractQueuedSynchronizer {
19+
Sync(int count) {
20+
if (count <= 0) {
21+
throw new IllegalArgumentException("count must larger than zero.");
22+
}
23+
setState(count);
24+
}
25+
26+
/**
27+
* 尝试共享式获取同步状态
28+
* @param reduceCount
29+
* @return
30+
*/
31+
@Override
32+
public int tryAcquireShared(int reduceCount) {
33+
for (;;) {
34+
// 获取同步状态
35+
int current = getState();
36+
int newCount = current - reduceCount;
37+
38+
if (newCount < 0 || compareAndSetState(current, newCount)) {
39+
return newCount;
40+
}
41+
}
42+
}
43+
44+
/**
45+
* 共享式释放同步状态
46+
* @param returnCount
47+
* @return
48+
*/
49+
@Override
50+
public boolean tryReleaseShared(int returnCount) {
51+
for (;;) {
52+
int current = getState();
53+
int newCount = current + returnCount;
54+
if (compareAndSetState(current, newCount)) {
55+
return true;
56+
}
57+
}
58+
}
59+
}
60+
61+
/**
62+
* 加锁
63+
*/
64+
@Override
65+
public void lock() {
66+
sync.acquireShared(1);
67+
}
68+
69+
/**
70+
* 释放锁
71+
*/
72+
@Override
73+
public void unlock() {
74+
sync.releaseShared(1);
75+
}
76+
77+
@Override
78+
public void lockInterruptibly() throws InterruptedException {
79+
80+
}
81+
82+
@Override
83+
public boolean tryLock() {
84+
return false;
85+
}
86+
87+
@Override
88+
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
89+
return false;
90+
}
91+
92+
@Override
93+
public Condition newCondition() {
94+
return null;
95+
}
96+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.learnjava.concurrent;
2+
3+
import com.learnjava.concurent.TwinsLock;
4+
import org.junit.Test;
5+
6+
import java.util.concurrent.locks.Lock;
7+
8+
/**
9+
* @author LuoHaiYang
10+
*/
11+
public class TwinsLockTeset {
12+
13+
@Test
14+
public void test() {
15+
final Lock lock = new TwinsLock();
16+
class Worker extends Thread {
17+
public void run() {
18+
while (true) {
19+
lock.lock();
20+
try {
21+
Thread.sleep(1000);
22+
System.out.println(Thread.currentThread().getName());
23+
Thread.sleep(1000);
24+
} catch (Exception e) {
25+
e.printStackTrace();
26+
} finally {
27+
lock.unlock();
28+
}
29+
}
30+
}
31+
}
32+
33+
// 启动10个线程
34+
for (int i = 0; i < 10; i++) {
35+
Worker worker = new Worker();
36+
worker.setName("Thread-" + i);
37+
//worker.setDaemon(true);
38+
worker.start();
39+
}
40+
41+
// 每隔10秒换行
42+
for (int i = 0; i < 10; i++) {
43+
try {
44+
Thread.sleep(1000);
45+
System.out.println();
46+
} catch (Exception e) {
47+
e.printStackTrace();
48+
}
49+
}
50+
}
51+
}

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@
2121
- [深入SpringIOC容器二](https://blog.csdn.net/CoderBruis/article/details/86505582)
2222

2323
- SpringAOP源码学习
24-
- Spring版本:5.2.1.RELEASE
25-
26-
- 超大超全Spring源码知识脑图
27-
28-
![image](https://github.com/coderbruis/JavaSourceLearning/blob/master/note/images/SpringBoot.png)
24+
- Spring版本:5.2.1.RELEASE
2925

3026
- [深入学习SpringAOP源码(一)——注册AnnotationAwareAspectJAutoProxyCreator](https://blog.csdn.net/CoderBruis/article/details/100031756)
3127
- [深入学习SpringAOP源码(二)—— 深入AnnotationAwareAspectJAutoProxyCreator](https://blog.csdn.net/CoderBruis/article/details/100042081)
3228
- [深入学习SpringAOP源码(三)——揭开JDK动态代理和CGLIB代理的神秘面纱](https://blog.csdn.net/CoderBruis/article/details/100083575)
3329

3430
- SpringBoot源码学习
3531
- SpringBoot版本:2.2.1.RELEASE
32+
33+
- 超大超全Spring源码知识脑图
34+
35+
![image](https://github.com/coderbruis/JavaSourceLearning/blob/master/note/images/SpringBoot.png)
36+
37+
-[深入SpringBoot源码学习之——SpringFactoriesLoader](https://blog.csdn.net/CoderBruis/article/details/106559304)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Initializer
2-
org.springframework.context.ApplicationContextInitializer=com.bruis.learnsb.initializer.FirstInitializer
2+
# org.springframework.context.ApplicationContextInitializer=com.bruis.learnsb.initializer.FirstInitializer
33

44
# listener
5-
org.springframework.context.ApplicationListener=com.bruis.learnsb.listener.FirstListener
5+
# org.springframework.context.ApplicationListener=com.bruis.learnsb.listener.FirstListener
166 KB
Loading

0 commit comments

Comments
 (0)