-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathSemapDemo.java
More file actions
29 lines (25 loc) · 786 Bytes
/
Copy pathSemapDemo.java
File metadata and controls
29 lines (25 loc) · 786 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
package ch3.s1;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class SemapDemo implements Runnable {
//5个一组输出
final Semaphore semp = new Semaphore(5);
public void run() {
try {
semp.acquire();
Thread.sleep(2000);
System.out.println(Thread.currentThread().getId() + " done!");
semp.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String []args){
ExecutorService exec = Executors.newFixedThreadPool(20);
final SemapDemo demo = new SemapDemo();
for(int i=0;i<20;i++){
exec.submit(demo);
}
}
}