JDK 5.0 Concurrency Utilities 并发处理(4)Semaphore 信号量
Semaphore 信号量
1
package com.vinko.test.concurrent;
2![](/Images/OutliningIndicators/None.gif)
3
import java.util.Calendar;
4
import java.util.concurrent.Semaphore;
5
import java.util.concurrent.locks.Condition;
6
import java.util.concurrent.locks.Lock;
7
import java.util.concurrent.locks.ReentrantLock;
8![](/Images/OutliningIndicators/None.gif)
9
public class TestSemaphore {
10![](/Images/OutliningIndicators/InBlock.gif)
11
private ReentrantLock lock = null;
12
private Condition condition = null;
13
private Semaphore semaphore = null;
14
15
public TestSemaphore() {
16
lock = new ReentrantLock();
17
condition = lock.newCondition();
18
semaphore = new Semaphore(2, true);
19
}
20
21
/**
22
* @param args
23
*/
24
public static void main(String[] args) {
25![](/Images/OutliningIndicators/InBlock.gif)
26
TestSemaphore tester = new TestSemaphore();
27
28
tester.test();
29
}
30
31
public Lock getLock() {
32
return lock;
33
}
34
35
public Condition getCondition() {
36
return condition;
37
}
38
39
public Semaphore getSemaphore() {
40
return semaphore;
41
}
42
43
public void test() {
44
try {
45
/*
46
semaphore.acquire();
47
System.out.println("get semaphore");
48
49
semaphore.acquire();
50
System.out.println("get semaphore");
51![](/Images/OutliningIndicators/InBlock.gif)
52
semaphore.release();
53![](/Images/OutliningIndicators/InBlock.gif)
54
semaphore.acquire();
55
System.out.println("get semaphore");
56
57
semaphore.acquire();
58
System.out.println("get semaphore");
59
*/
60
61
new Thread(new TestThread(this)).start();
62
new Thread(new TestThread(this)).start();
63
new Thread(new TestThread(this)).start();
64
new Thread(new TestThread(this)).start();
65
66
Thread.sleep(3000);
67
68
lock.lock();
69
condition.signal();
70
// condition.signal();
71
// condition.signalAll();
72
lock.unlock();
73
74
} catch (InterruptedException e) {
75
e.printStackTrace();
76
}
77
}
78![](/Images/OutliningIndicators/InBlock.gif)
79
}
80![](/Images/OutliningIndicators/None.gif)
81
class TestThread implements Runnable {
82
83
private TestSemaphore tester = null;
84
85
public TestThread(TestSemaphore tester) {
86
this.tester = tester;
87
}
88
89
public void run() {
90
91
Calendar now = Calendar.getInstance();
92
93
System.out.println(now.getTime() + " " + Thread.currentThread() + " started.");
94![](/Images/OutliningIndicators/InBlock.gif)
95
while (true) {
96
try {
97
tester.getLock().lock();
98
tester.getCondition().await();
99
tester.getLock().unlock();
100
101
Calendar now1 = Calendar.getInstance();
102
System.out.println(now1.getTime() + " " + Thread.currentThread() + " got signal.");
103
104
tester.getSemaphore().acquire();
105
106
Calendar now2 = Calendar.getInstance();
107
System.out.println(now2.getTime() + " " + Thread.currentThread() + " got semaphore.");
108
109
// tester.getSemaphore().release();
110
111
} catch (InterruptedException e) {
112
e.printStackTrace();
113
}
114
}
115
}
116
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
77
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
80
![](/Images/OutliningIndicators/None.gif)
81
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/InBlock.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
90
![](/Images/OutliningIndicators/InBlock.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/InBlock.gif)
95
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
96
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/InBlock.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/InBlock.gif)
102
![](/Images/OutliningIndicators/InBlock.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/InBlock.gif)
107
![](/Images/OutliningIndicators/InBlock.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
114
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
115
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
116
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)