Skip to content

Commit d953aa0

Browse files
committed
add > juc lock test code
1 parent fd2bece commit d953aa0

3 files changed

Lines changed: 199 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package juc.lock;
2+
3+
import java.util.concurrent.locks.Condition;
4+
import java.util.concurrent.locks.Lock;
5+
import java.util.concurrent.locks.ReentrantLock;
6+
7+
/**
8+
* Created by 李恒名 on 2017/6/18.
9+
*/
10+
public class ConditionTest {
11+
private final Lock lock = new ReentrantLock();
12+
private final Condition condition = lock.newCondition();
13+
14+
15+
public void work() {
16+
lock.lock();
17+
try {
18+
try {
19+
System.out.println("Begin Work");
20+
condition.await();
21+
System.out.println("Begin End");
22+
} catch (InterruptedException e) {
23+
e.printStackTrace();
24+
}
25+
} finally {
26+
lock.unlock();
27+
}
28+
}
29+
30+
public void continueWork() {
31+
lock.lock();
32+
try {
33+
condition.signalAll();
34+
} finally {
35+
lock.unlock();
36+
}
37+
}
38+
39+
public static void main(String[] args) throws InterruptedException {
40+
ConditionTest test = new ConditionTest();
41+
new Thread(() -> test.work()).start();
42+
43+
//等待3000毫秒后唤醒,继续工作。
44+
Thread.sleep(3000);
45+
test.continueWork();
46+
}
47+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package juc.lock;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* Created by 李恒名 on 2017/6/17.
8+
*/
9+
public class ReentrantLockTest {
10+
11+
private final Lock lock = new ReentrantLock();
12+
private String content = "Old";
13+
14+
public void write() {
15+
lock.lock();
16+
//由于ReentrantLock是可重入锁,所以可以重复的加锁。
17+
//lock.lock();
18+
//lock.lock();
19+
System.out.println(Thread.currentThread() +" LOCK");
20+
try {
21+
try {
22+
//模拟方法需要执行100毫秒
23+
Thread.sleep(100);
24+
} catch (InterruptedException e) {
25+
e.printStackTrace();
26+
}
27+
content = "New";
28+
System.out.println(Thread.currentThread() +" Write content to: " + content);
29+
} finally {
30+
System.out.println(Thread.currentThread() +" UNLOCK");
31+
lock.unlock();
32+
//进行多少次加锁操作,也需要对应多少次解锁操作。
33+
//lock.unlock();
34+
//lock.unlock();
35+
}
36+
}
37+
38+
public void read() {
39+
lock.lock();
40+
System.out.println(Thread.currentThread() +" LOCK");
41+
try {
42+
try {
43+
//模拟方法需要执行100毫秒
44+
Thread.sleep(100);
45+
} catch (InterruptedException e) {
46+
e.printStackTrace();
47+
}
48+
System.out.println(Thread.currentThread() +" Read content is: " + content);
49+
} finally {
50+
System.out.println(Thread.currentThread() +" UNLOCK");
51+
lock.unlock();
52+
}
53+
}
54+
55+
56+
public static void main(String[] args) {
57+
final ReentrantLockTest test = new ReentrantLockTest();
58+
// 使用Java 8 lambda 简化代码
59+
new Thread(() -> test.write()).start();
60+
new Thread(() -> test.read()).start();
61+
new Thread(() -> test.read()).start();
62+
63+
/**
64+
不使用锁的输出:
65+
Thread[Thread-1,5,main] Read content is: Old
66+
Thread[Thread-2,5,main] Read content is: Old
67+
Thread[Thread-0,5,main] Write content to: New
68+
69+
使用锁后:
70+
Thread[Thread-0,5,main] LOCK
71+
Thread[Thread-0,5,main] Write content to: New
72+
Thread[Thread-0,5,main] UNLOCK
73+
Thread[Thread-1,5,main] LOCK
74+
Thread[Thread-1,5,main] Read content is: New
75+
Thread[Thread-1,5,main] UNLOCK
76+
Thread[Thread-2,5,main] LOCK
77+
Thread[Thread-2,5,main] Read content is: New
78+
Thread[Thread-2,5,main] UNLOCK
79+
**/
80+
}
81+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package juc.lock;
2+
3+
import java.util.concurrent.locks.ReadWriteLock;
4+
import java.util.concurrent.locks.ReentrantReadWriteLock;
5+
6+
/**
7+
* Created by 李恒名 on 2017/6/17.
8+
*/
9+
public class ReentrantReadWriteLockTest {
10+
11+
private final ReadWriteLock lock = new ReentrantReadWriteLock();
12+
private String content = "Old";
13+
14+
public void write() {
15+
lock.writeLock().lock();
16+
System.out.println(Thread.currentThread() +" LOCK");
17+
try {
18+
try {
19+
//模拟方法需要执行100毫秒
20+
Thread.sleep(100);
21+
} catch (InterruptedException e) {
22+
e.printStackTrace();
23+
}
24+
content = "New";
25+
System.out.println(Thread.currentThread() +" Write content to: " + content);
26+
} finally {
27+
System.out.println(Thread.currentThread() +" UNLOCK");
28+
lock.writeLock().unlock();
29+
}
30+
}
31+
32+
public void read() {
33+
lock.readLock().lock();
34+
System.out.println(Thread.currentThread() +" LOCK");
35+
try {
36+
try {
37+
//模拟方法需要执行100毫秒
38+
Thread.sleep(100);
39+
} catch (InterruptedException e) {
40+
e.printStackTrace();
41+
}
42+
System.out.println(Thread.currentThread() +" Read content is: " + content);
43+
} finally {
44+
System.out.println(Thread.currentThread() +" UNLOCK");
45+
lock.readLock().unlock();
46+
}
47+
}
48+
49+
50+
public static void main(String[] args) {
51+
final ReentrantReadWriteLockTest test = new ReentrantReadWriteLockTest();
52+
// 使用Java 8 lambda 简化代码
53+
new Thread(() -> test.write()).start();
54+
new Thread(() -> test.read()).start();
55+
new Thread(() -> test.read()).start();
56+
57+
/**
58+
输出:
59+
Thread[Thread-0,5,main] LOCK
60+
Thread[Thread-0,5,main] Write content to: New
61+
Thread[Thread-0,5,main] UNLOCK
62+
Thread[Thread-1,5,main] LOCK
63+
Thread[Thread-2,5,main] LOCK
64+
Thread[Thread-1,5,main] Read content is: New
65+
Thread[Thread-2,5,main] Read content is: New
66+
Thread[Thread-1,5,main] UNLOCK
67+
Thread[Thread-2,5,main] UNLOCK
68+
可以看到两个线程在读的时候可以同时获得锁
69+
**/
70+
}
71+
}

0 commit comments

Comments
 (0)