Skip to content

Commit c82ea18

Browse files
committed
스레드 안정성
1 parent f0690d8 commit c82ea18

File tree

7 files changed

+22
-3
lines changed

7 files changed

+22
-3
lines changed

src/main/java/io/concurrency/chapter05/exam02/CpuNonSyncExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ public static void main(String[] args) throws InterruptedException {
2525
thread2.join();
2626

2727
System.out.println("예상 결과: " + (2 * ITERATIONS));
28-
System.out.println("실제 결과: " + count);
28+
System.out.println("실제 결과: " + count); //CPU가 동기화되지 않은 값을 보게 됨
2929
}
3030
}

src/main/java/io/concurrency/chapter05/exam03/CriticalSectionExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.concurrency.chapter05.exam03;
22

33
public class CriticalSectionExample {
4-
public static void main(String[] args) {
4+
public static void main(String[] args) {
55
SharedResource resource = new SharedResource();
66

77
Thread t1 = new Thread(resource::increment);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## 임계 구역과 경쟁 상태
2+
임계 구역과 경쟁 상태는 뗄 수 없는 관계이다
3+
4+
경재 상태에 들어가려면 임계 구역이 있어야 되는 거고, 임계 구역은 경쟁 상태를 만들게 된다
5+
따라서 하나의 영역 안에서 두 개의 일들이 발생할 수 있다
6+

src/main/java/io/concurrency/chapter05/exam04/ImmutableExample.java

+4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ public static void main(String[] args) {
1616

1717
ImmutablePerson person = new ImmutablePerson("홍길동", 25);
1818

19+
//여러 스레드에서 하나의 참조 객체를 공유하게 되면 잘못된 결과가 나올 수 있다
20+
//그러나 이 경우는 스레드 안전한데,
21+
//그 이유는 공유하고 있는 참조 객체가 final, 즉 불변 객체이기 때문이다
1922
for (int i = 0; i < 10; i++) {
2023
new Thread(new ImmutableExample(person)).start();
2124
}
2225
}
2326
}
2427

28+
//상속 못 함
2529
final class ImmutablePerson {
2630
private final String name;
2731
private final int age;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
여러 스레드가 같은 변수를 바라보게 되면 스레드에 안전하지 않게 된다
3+
4+
1) 스레드 안정성 시나리오
5+
- 메서드 내에서 지역 객체 참조 -> 각 스레드의 스택에 독립적으로 생성되기 때문
6+
- 다른 주소 값을 갖는 멤버 변수를 다루는 경우
7+
- 같은 주소 값을 갖는 멤버 변수를 다루더라도, 그 멤버 변수에 접근하는 것이 synchronized일 때
8+
- 같은 주소 값을 갖는 멤버 변수를 다루더라고, 그 멤버 변수가 불변 객체일 때

src/main/java/io/concurrency/chapter05/exam04/ThreadSafeLocalVariableExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class ThreadSafeLocalVariableExample {
44

5-
// int localSum = 0;
5+
//int localSum = 0;
66
public void printNumbers(int plus) {
77
// 지역 변수, 매개 변수로 정의된 변수. 각 스레드는 이 변수의 독립된 복사본을 가짐.
88
int localSum = 0;

src/main/java/io/concurrency/chapter05/exam04/ThreadSafeMemberReferenceObjectExample.java

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static void main(String[] args) throws InterruptedException {
1111
Company company = new Company("Company"); // 스레드에 안전하지 못함, 멤버변수를 공유함
1212
new Thread(new MyRunnable(company)).start();
1313
new Thread(new MyRunnable(company)).start();
14+
1415
}
1516
}
1617

0 commit comments

Comments
 (0)