-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockDemo1.java
More file actions
57 lines (46 loc) · 1.22 KB
/
Copy pathLockDemo1.java
File metadata and controls
57 lines (46 loc) · 1.22 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package four;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author yangxp
* @date 2017年8月10日 上午9:13:22
* <p>
* 展现lock的同步效果,一个线程打印完了,其他线程才可以继续打印。
*/
public class LockDemo1 {
public static void main(String[] args) {
MyServcie1 myServcie1 = new MyServcie1();
MyThread1 a1 = new MyThread1(myServcie1);
MyThread1 a2 = new MyThread1(myServcie1);
MyThread1 a3 = new MyThread1(myServcie1);
MyThread1 a4 = new MyThread1(myServcie1);
MyThread1 a5 = new MyThread1(myServcie1);
a1.start();
a2.start();
a3.start();
a4.start();
a5.start();
}
}
class MyServcie1 {
private Lock lock = new ReentrantLock();
public void testMethod() {
System.out.println("lock 之上");
lock.lock();
for (int i = 0; i < 5; i++) {
System.out.println("ThreadName = " + Thread.currentThread().getName());
}
lock.unlock();
}
}
class MyThread1 extends Thread {
private MyServcie1 myServcie1;
public MyThread1(MyServcie1 myServcie1) {
super();
this.myServcie1 = myServcie1;
}
@Override
public void run() {
myServcie1.testMethod();
}
}