forked from janzolau1987/study-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwinsLockTest.java
More file actions
43 lines (39 loc) · 844 Bytes
/
TwinsLockTest.java
File metadata and controls
43 lines (39 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.yaoyaohao.study.thread;
import java.util.concurrent.locks.Lock;
/**
* TwinsLockTest
*
* @author liujianzhu
* @date 2016年8月1日 下午5:43:50
*/
public class TwinsLockTest {
public static void main(String[] args) {
final Lock lock = new TwinsLock();
class Worker extends Thread {
@Override
public void run() {
//while (true) {
lock.lock();
try {
SleepUtils.second(1);
System.out.println(Thread.currentThread().getName());
SleepUtils.second(1);
} finally {
lock.unlock();
}
//}
}
}
for (int i = 0; i < 10; i++) {
Worker w = new Worker();
w.setName("Thread-" + i);
w.setDaemon(true);
w.start();
}
//
for (int i = 0; i < 10; i++) {
SleepUtils.second(1);
System.out.println();
}
}
}