-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava210_thread.java
More file actions
77 lines (65 loc) · 1.78 KB
/
Copy pathJava210_thread.java
File metadata and controls
77 lines (65 loc) · 1.78 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package java0914_thread;
/*
* 동기화(synchronized)
*
* 계좌번호 => 공유자원
* A지점, B지점, C지점 => 스레드
*
* 동기화 : 하나의 스레드가 공유자원을 사용하고 있으면 다른 스레드가 접근하는 것을 막아주는 기능이다.
*
* 동기화 목적 : 데이터의 일관성 유지를 위해서이다.
* 동기화 키워드 : synchronized
*
* 동기화 설정방법
* 1) 메소드에 lock을 걸고자 할때
* synchronized void test( ) { }
* 2) 특정한 객체에 lock을 걸고자 할때
* void test(){
* synchronized(객체의 참조변수){ }
* }
*
* wait(), notify(), notifyAll()메소드 동기화가 설정되어 있는 영역에서만 호출 할 수 있다.
*/
class Washroom {
synchronized void openDoor(String name) {
System.out.println(name + "님이 입장");
for (int i = 0; i < 50000; i++) {
if (i % 10000 == 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + "님이 업무 보는 중");
}
}
System.out.println(name + "님이 퇴장");
}
}
class FamilyThread extends Thread {
private Washroom wr;
private String who;
public FamilyThread() {
}
public FamilyThread(Washroom wr, String who) {
this.wr = wr;
this.who = who;
}
@Override
public void run() {
wr.openDoor(who);
}
}
public class Java210_thread {
public static void main(String[] args) {
Washroom wr = new Washroom();
FamilyThread father = new FamilyThread(wr, "father");
FamilyThread mother = new FamilyThread(wr, "mother");
FamilyThread sister = new FamilyThread(wr, "sister");
FamilyThread brother = new FamilyThread(wr, "brother");
father.start();
mother.start();
sister.start();
brother.start();
}
}