K-DigitalTraining 강의/1. Java
[4주차] 75. Thread, 공유자원 사용시 동기화의 필요성(완성)
이대곤
2022. 6. 3. 15:29
* 동기화란?
두개 이상의 쓰레드가 공유자원을 사용할 때 한 사람씩만 순차적으로 접근하도록 하는 기법이다. 동시에 두 쓰레드가 공유자원에 접근하게 될 경우 한쪽만 접근이 허용되며 한 쪽은 앞사람이 일을 마칠 때 까지 대기상태가 된다. 공유자원을 동기화해주지 않을 경우 발생하는 문제점은 아래의 예시 코드를 통해 알 수 있다.
* 동기화 시켜주는 방법 두가지
1) synchronized 키워드를 동기화 시키고자 하는 메서드에 붙여서 메서드를 통째로 동기화 시키는 것
2) synchronized 키워드를 블록화 해서 일부 영역만 동기화 시키는 것
* 임계영역이란?
다른 주체가 접근중일 때 접근하지 못하도록 제한하는 영역으로 synchronized 키워드를 이용해 설정한다.
NotSync로 공유자원 사용시 발생할 수 있는 문제점 예시
아래 코드에서 공유하는 i를 AAA, BBB 쓰레드에서 각각 10만번씩 증가시키기 때문에 어느 한 쪽이 i를 출력할 땐 20만이 나와야 하는게 상식과 맞는데 그렇지가 않다. 그 이유는 AAA, BBB 두 쓰레드가 공유하는 i에 동시에 접근해서 뭔가를 하려고 했던 경우 한쪽의 시도가 무효화 되었기 때문이다. 이것이 공유자원 사용시 동기화가 필요한 이유이다.
class Data1{
int i = 0;
}//
class AAA extends Thread{
public void run() {
for(int i = 0; i< 100000; i++) {
Ex10_06_NotSyncTest.d1.i++; // 임계영역
}
System.out.println("AAA i: " + Ex10_06_NotSyncTest.d1.i);
}
}//
class BBB extends Thread{
public void run() {
for(int i = 0; i< 100000; i++) {
Ex10_06_NotSyncTest.d1.i++; // 임계영역
}
System.out.println("BBB i: " + Ex10_06_NotSyncTest.d1.i);
}
}//
public class Ex10_06_NotSyncTest {
static Data1 d1 = new Data1();
public static void main(String[] args) {
System.out.println("main 시작");
AAA a = new AAA();
BBB b = new BBB();
a.start();
b.start();
System.out.println("main 종료");
}
}
main 시작
main 종료
AAA i: 119220
BBB i: 134440Sync로 공유자원 사용시 발생할 수 있는 문제점 예시
Sync로 공유자원 사용하도록 수정하여 문제점 수정
class Data2{
int i = 0;
}//
class AAA2 extends Thread{
public void run() {
for(int i = 0; i< 100000; i++) {
synchronized(Ex10_07_SyncTest.d1) {
Ex10_07_SyncTest.d1.i++; // 임계영역
}
}
System.out.println("AAA2 i: " + Ex10_07_SyncTest.d1.i);
}
}//
class BBB2 extends Thread{
public void run() {
for(int i = 0; i< 100000; i++) {
synchronized(Ex10_07_SyncTest.d1) {
Ex10_07_SyncTest.d1.i++; // 임계영역
}
}
System.out.println("BBB2 i: " + Ex10_07_SyncTest.d1.i);
}
}//
public class Ex10_07_SyncTest {
static Data2 d1 = new Data2();
public static void main(String[] args) {
System.out.println("main 시작");
AAA2 a = new AAA2();
BBB2 b = new BBB2();
a.start();
b.start();
System.out.println("main 종료");
}
}
class Data2{
int i = 0;
}//
class AAA2 extends Thread{
public void run() {
for(int i = 0; i< 100000; i++) {
synchronized(Ex10_07_SyncTest.d1) {
Ex10_07_SyncTest.d1.i++; // 임계영역
}
}
System.out.println("AAA2 i: " + Ex10_07_SyncTest.d1.i);
}
}//
class BBB2 extends Thread{
public void run() {
for(int i = 0; i< 100000; i++) {
synchronized(Ex10_07_SyncTest.d1) {
Ex10_07_SyncTest.d1.i++; // 임계영역
}
}
System.out.println("BBB2 i: " + Ex10_07_SyncTest.d1.i);
}
}//
public class Ex10_07_SyncTest {
static Data2 d1 = new Data2();
public static void main(String[] args) {
System.out.println("main 시작");
AAA2 a = new AAA2();
BBB2 b = new BBB2();
a.start();
b.start();
System.out.println("main 종료");
}
}
main 시작
main 종료
AAA2 i: 198168
BBB2 i: 200000