-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemaphoreDemo.java
More file actions
55 lines (43 loc) · 1.52 KB
/
SemaphoreDemo.java
File metadata and controls
55 lines (43 loc) · 1.52 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
package kb.concurrent.synchronizers;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
/**
* Represents a that makes IO calls to slow service.
*/
class IOTask implements Runnable {
private Semaphore semaphore;
public IOTask(Semaphore semaphore) {
this.semaphore = semaphore;
}
@Override
public void run() {
try {
// System.out.println(semaphore.availablePermits());
semaphore.acquire();
System.out.println("Acquire: " + Thread.currentThread().getName());
Thread.sleep(1000);
System.out.println("Release: " + Thread.currentThread().getName());
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class SemaphoreDemo {
public static void main(String[] args) {
// We have a pool with 20 threads but because of slow service we have a
// limitation of 3 available permits to connect to this service.
ExecutorService executor = Executors.newFixedThreadPool(20);
Semaphore semaphore = new Semaphore(3);
IntStream.range(0, 100).forEach(i -> executor.execute(new IOTask(semaphore)));
executor.shutdown();
try {
executor.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}