-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathUsingSemaphores.java
49 lines (43 loc) · 1.25 KB
/
UsingSemaphores.java
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
package br.com.leonardoz.features.synchronizers;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
/**
* Semaphores controls the number of activities that can access a resource or
* perform a certain action;
*
* - First you give a number of 'permits';
*
* - Activities will acquire it and release when they're done;
*
* - If none is available, activity will block until one become available.
*
* Good for resource pools.
*
*/
public class UsingSemaphores {
public static void main(String[] args) {
var executor = Executors.newCachedThreadPool();
var semaphore = new Semaphore(3);
Runnable r = () -> {
try {
System.out.println("Trying to acquire - " + Thread.currentThread().getName());
if (semaphore.tryAcquire(2, TimeUnit.SECONDS)) {
// use-get resource
// simulate work in progress
System.out.println("Acquired - " + Thread.currentThread().getName());
Thread.sleep(2000);
System.out.println("Done - " + Thread.currentThread().getName());
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
};
for (int i = 0; i < 4; i++) {
executor.execute(r);
}
executor.shutdown();
}
}