LockObject.java
1.24 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
58
59
package com.taobao.pamirs.schedule.taskmanager;
class LockObject {
private int m_threadCount = 0;
private Object m_waitOnObject = new Object();
public LockObject() {
}
public void waitCurrentThread() throws Exception {
synchronized (m_waitOnObject) {
// System.out.println(Thread.currentThread().getName() + ":" +
// "休眠当前线程");
this.m_waitOnObject.wait();
}
}
public void notifyOtherThread() throws Exception {
synchronized (m_waitOnObject) {
// System.out.println(Thread.currentThread().getName() + ":" +
// "唤醒所有等待线程");
this.m_waitOnObject.notifyAll();
}
}
public void addThread() {
synchronized (this) {
m_threadCount = m_threadCount + 1;
}
}
public void realseThread() {
synchronized (this) {
m_threadCount = m_threadCount - 1;
}
}
/**
* 降低线程数量,如果是最后一个线程,则不能休眠
*
* @return boolean
*/
public boolean realseThreadButNotLast() {
synchronized (this) {
if (this.m_threadCount == 1) {
return false;
} else {
m_threadCount = m_threadCount - 1;
return true;
}
}
}
public int count() {
synchronized (this) {
return m_threadCount;
}
}
}