JDK 5.0 Concurrency Utilities 并发处理(4)Semaphore 信号量
Semaphore 信号量
1
package com.vinko.test.concurrent;
2![]()
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![]()
9
public class TestSemaphore {
10![]()
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![]()
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![]()
52
semaphore.release();
53![]()
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![]()
79
}
80![]()
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![]()
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
}
package com.vinko.test.concurrent;2

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

9
public class TestSemaphore {10

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 args23
*/24
public static void main(String[] args) {25

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

52
semaphore.release();53

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

79
}80

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

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
}



浙公网安备 33010602011771号