forked from lastwhispers/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemapDemo.java
More file actions
34 lines (29 loc) · 897 Bytes
/
Copy pathSemapDemo.java
File metadata and controls
34 lines (29 loc) · 897 Bytes
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
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
/**
* @author lastwhisper
*/
public class SemapDemo implements Runnable {
final Semaphore semaphore = new Semaphore(5);
@Override
public void run() {
try {
semaphore.acquire();
Thread.sleep(2000);
System.out.println(Thread.currentThread().getId()+": done");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
semaphore.release();
}
}
public static void main(String[] args){
ExecutorService executorService = Executors.newFixedThreadPool(20);
SemapDemo semapDemo = new SemapDemo();
for (int i = 0; i < 20; i++) {
executorService.submit(semapDemo);
}
}
}