Skip to content

Commit a35ddb4

Browse files
author
fengyuan.wan
committed
lock
1 parent 442617b commit a35ddb4

File tree

4 files changed

+225
-10
lines changed

4 files changed

+225
-10
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package java0.conc0302.threadpool;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.locks.Condition;
5+
import java.util.concurrent.locks.Lock;
6+
import java.util.concurrent.locks.ReentrantLock;
7+
8+
/**
9+
* @Desc
10+
* @Author wfy
11+
* @Date 2021/2/4 10:38
12+
*/
13+
public class ConditionTest {
14+
static Lock lock = new ReentrantLock(true);
15+
static Condition fullCondition = lock.newCondition();
16+
static Condition emptyCondition = lock.newCondition();
17+
static int size = 0;
18+
static int capacity = 10;
19+
20+
//空了才入队。 满了才出队。
21+
public static void main(String[] args) {
22+
enqueue();
23+
dequeue();
24+
}
25+
26+
public static void enqueue() {
27+
new Thread(() -> {
28+
lock.lock();
29+
try {
30+
while (true) {
31+
if (size < capacity) {
32+
TimeUnit.MILLISECONDS.sleep(100);
33+
++size;
34+
System.out.println("enqueue " + size);
35+
} else {
36+
emptyCondition.signalAll();
37+
fullCondition.await();
38+
}
39+
}
40+
} catch (Exception e) {
41+
42+
} finally {
43+
lock.unlock();
44+
}
45+
46+
}).start();
47+
}
48+
49+
public static void dequeue() {
50+
new Thread(() -> {
51+
lock.lock();
52+
try {
53+
while (true) {
54+
if (size > 0) {
55+
TimeUnit.MILLISECONDS.sleep(100);
56+
--size;
57+
System.out.println("dequeue " + size);
58+
} else {
59+
fullCondition.signalAll();
60+
emptyCondition.await();
61+
}
62+
}
63+
} catch (Exception e) {
64+
65+
} finally {
66+
lock.unlock();
67+
}
68+
69+
}).start();
70+
}
71+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package java0.conc0302.threadpool;
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+
class Mutex implements Lock {
9+
// 静态内部类,自定义同步器
10+
private static class Sync extends AbstractQueuedSynchronizer {
11+
// 是否处于占用状态
12+
@Override
13+
protected boolean isHeldExclusively() {
14+
return getState() == 1;
15+
}
16+
17+
// 当状态为0的时候获取锁
18+
@Override
19+
public boolean tryAcquire(int acquires) {
20+
if (compareAndSetState(0, 1)) {
21+
setExclusiveOwnerThread(Thread.currentThread());
22+
return true;
23+
}
24+
return false;
25+
}
26+
27+
// 释放锁,将状态设置为0
28+
@Override
29+
protected boolean tryRelease(int releases) {
30+
if (getState() == 0) throw new IllegalMonitorStateException();
31+
setExclusiveOwnerThread(null);
32+
setState(0);
33+
return true;
34+
}
35+
36+
// 返回一个Condition,每个condition都包含了一个condition队列
37+
Condition newCondition() {
38+
return new ConditionObject();
39+
}
40+
}
41+
42+
// 仅需要将操作代理到Sync上即可
43+
private final Sync sync = new Sync();
44+
45+
@Override
46+
public void lock() {
47+
sync.acquire(1);
48+
}
49+
50+
@Override
51+
public boolean tryLock() {
52+
return sync.tryAcquire(1);
53+
}
54+
55+
@Override
56+
public void unlock() {
57+
sync.release(1);
58+
}
59+
60+
@Override
61+
public Condition newCondition() {
62+
return sync.newCondition();
63+
}
64+
65+
public boolean isLocked() {
66+
return sync.isHeldExclusively();
67+
}
68+
69+
public boolean hasQueuedThreads() {
70+
return sync.hasQueuedThreads();
71+
}
72+
73+
@Override
74+
public void lockInterruptibly() throws InterruptedException {
75+
sync.acquireInterruptibly(1);
76+
}
77+
78+
@Override
79+
public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
80+
return sync.tryAcquireNanos(1, unit.toNanos(timeout));
81+
}
82+
}
Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package java0.conc0302.threadpool;
22

3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.locks.Condition;
35
import java.util.concurrent.locks.Lock;
46
import java.util.concurrent.locks.ReentrantLock;
57

@@ -13,22 +15,39 @@ public class ReentrantLockTest {
1315
static int count = 0;
1416

1517
public static void main(String[] args) {
16-
for (int i = 0; i < 10; i++) {
17-
new Thread(ReentrantLockTest::incr).start();
18+
Thread interT = null;
19+
for (int i = 0; i < 2; i++) {
20+
Thread t = new Thread(ReentrantLockTest::incr);
21+
t.start();
22+
if (i == 1) {
23+
interT = t;
24+
}
1825
}
19-
26+
// try {
27+
// TimeUnit.SECONDS.sleep(1);
28+
// } catch (InterruptedException e) {
29+
// e.printStackTrace();
30+
// }
2031
}
2132

2233
public static void incr() {
23-
lock.lock();
34+
boolean locked = false;
2435
try {
25-
for (int i = 0; i < 10000; i++) {
26-
count++;
27-
System.out.println(count);
36+
locked = lock.tryLock(10, TimeUnit.MILLISECONDS);
37+
} catch (InterruptedException e) {
38+
System.out.println("error");
39+
e.printStackTrace();
40+
}
41+
if (locked) {
42+
Condition c = lock.newCondition();
43+
try {
44+
for (int i = 0; i < 100000; i++) {
45+
count++;
46+
System.out.println(count);
47+
}
48+
} finally {
49+
lock.unlock();
2850
}
29-
} finally {
30-
lock.unlock();
3151
}
32-
3352
}
3453
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package java0.conc0302.threadpool;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.locks.Condition;
5+
import java.util.concurrent.locks.Lock;
6+
import java.util.concurrent.locks.ReentrantLock;
7+
8+
/**
9+
* @Desc
10+
* @Author wfy
11+
* @Date 2021/2/3 18:05
12+
*/
13+
public class ReentrantLockTest01 {
14+
static ReentrantLock lock = new ReentrantLock(true);
15+
static int count = 0;
16+
17+
public static void main(String[] args) {
18+
for (int i = 0; i < 1; i++) {
19+
Thread t = new Thread(ReentrantLockTest01::incr);
20+
t.start();
21+
}
22+
// try {
23+
// TimeUnit.SECONDS.sleep(1);
24+
// } catch (InterruptedException e) {
25+
// e.printStackTrace();
26+
// }
27+
}
28+
29+
public static void incr() {
30+
System.out.println(lock.getHoldCount());
31+
lock.lock();
32+
lock.lock();
33+
lock.lock();
34+
try {
35+
System.out.println(lock.getHoldCount());
36+
} finally {
37+
lock.unlock();
38+
lock.unlock();
39+
lock.unlock();
40+
}
41+
System.out.println(lock.getHoldCount());
42+
}
43+
}

0 commit comments

Comments
 (0)