forked from brianway/java-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun5_getQueueLength.java
More file actions
43 lines (33 loc) · 951 Bytes
/
Copy pathRun5_getQueueLength.java
File metadata and controls
43 lines (33 loc) · 951 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.brianway.learning.java.multithread.lock.example5;
/**
* Created by Brian on 2016/4/15.
*/
/**
* P220
* 测试方法int getQueueLength()
* 返回正在等待获取此锁定的线程估计数
*/
public class Run5_getQueueLength {
public static void main(String[] args) throws InterruptedException {
final Service2 service = new Service2();
Runnable runnable = new Runnable() {
public void run() {
service.serviceMethod1();
}
};
Thread[] threads = new Thread[10];
for (int i = 0; i < 10; i++) {
threads[i] = new Thread(runnable);
}
for (int i = 0; i < 10; i++) {
threads[i].start();
}
Thread.sleep(2000);
System.out.println("有线程数:" + service.lock.getQueueLength() + "在等待获取锁");
}
}
/*
输出:
ThreadName=Thread-0
有线程数:9在等待获取锁
*/