-
Notifications
You must be signed in to change notification settings - Fork 0
/
Semaphore.java
48 lines (36 loc) · 962 Bytes
/
Semaphore.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
import java.util.concurrent.*;
class Shared {
static int count = 0;
}
class Schedule implements Runnable {
Semaphore sem;
Schedule(Semaphore se){
sem=se;
}
@Override
public void run(){
try{
sem.acquire();
for(int i=1;i<=5;i++){
System.out.println(i+" "+Thread.currentThread().getId());
Thread.sleep(1000);
}
sem.release();
}catch(Exception e){
}
}
}
class HelloWorld {
public static void main(String[] args) {
Semaphore sem = new Semaphore(1);
Thread t1=new Thread(new Schedule(sem));
Thread t2=new Thread(new Schedule(sem));
t1.start();
t2.start();
try{
t1.join();
t2.join();
}catch(Exception e){
}
}
}