-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSem.java
52 lines (47 loc) · 1.74 KB
/
Sem.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
50
51
52
import java.util.concurrent.Semaphore;
public class Sem {
static int sharedData = 0; // 공유 데이터
// 세마포 생성, 공유 자원 1개
static Semaphore semaphore = new Semaphore(1);
public static void main(String[] args) {
Thread thread1 = new Thread(new Increment());
Thread thread2 = new Thread(new Decrement());
thread1.start(); // 첫 번째 스레드 시작
thread2.start(); // 두 번째 스레드 시작
try {
thread1.join(); // 첫 번째 스레드 종료 대기
thread2.join(); // 두 번째 스레드 종료 대기
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Final value of sharedData: " + sharedData);
}
static class Increment implements Runnable {
public void run() {
for (int i = 0; i < 100000; i++) {
try {
semaphore.acquire(); // 세마포 획득
sharedData++; // 공유 데이터 증가
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release(); // 세마포 해제
}
}
}
}
static class Decrement implements Runnable {
public void run() {
for (int i = 0; i < 100000; i++) {
try {
semaphore.acquire(); // 세마포 획득
sharedData--; // 공유 데이터 감소
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release(); // 세마포 해제
}
}
}
}
}