mojo's Blog

Thread Synchronization 본문

Java

Thread Synchronization

_mojo_ 2021. 8. 9. 00:19

Synchronized를 사용하여 잘 동작하는 직계판을 응용하는 code

 

import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;

public class SynchronizedEx extends JFrame{

	class SharedBoard{
		private int sum = 0;
		synchronized public void add() {
			int n = sum;
			Thread.yield(); // 현재 실행 중인 스레드에게 양보하는 메소드
			n += 10;
			sum = n;
			System.out.println(Thread.currentThread().getName()+" : "+sum);
		}
		
		public int getSum() {
			return sum;
		}
	}
	
	class StudentThread extends Thread{
		private SharedBoard board;
		
		public StudentThread(String name, SharedBoard board) {
			super(name);
			this.board = board;
		}
		
		public void run() {
			for(int i=0;i<10;i++) {
				board.add();
			}
		}
	}
	
	SynchronizedEx(){
		SharedBoard board = new SharedBoard();
		
		Thread th1 = new StudentThread("kitae", board);
		Thread th2 = new StudentThread("hyosoo", board);
		
		th1.start();
		th2.start();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new  SynchronizedEx();
	}

}

Kitae와 hyosoo가 각각 10번씩 add()를 호출하였으므로 동기화(synchronization)가 잘 이루어져서 최종 누적 점수 sum이 200이 된다.

 

 

synchronized를 사용하지 않아 정상적으로 작동하지 않는 집계판 응용 code

 

import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;

public class SynchronizedEx extends JFrame{

	class SharedBoard{
		private int sum = 0;
		public void add() {
			int n = sum;
			Thread.yield(); // 현재 실행 중인 스레드에게 양보하는 메소드
			n += 10;
			sum = n;
			System.out.println(Thread.currentThread().getName()+" : "+sum);
		}
		
		public int getSum() {
			return sum;
		}
	}
	
	class StudentThread extends Thread{
		private SharedBoard board;
		
		public StudentThread(String name, SharedBoard board) {
			super(name);
			this.board = board;
		}
		
		public void run() {
			for(int i=0;i<10;i++) {
				board.add();
			}
		}
	}
	
	SynchronizedEx(){
		SharedBoard board = new SharedBoard();
		
		Thread th1 = new StudentThread("kitae", board);
		Thread th2 = new StudentThread("hyosoo", board);
		
		th1.start();
		th2.start();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new  SynchronizedEx();
	}

}

synchronized 를 삭제하고 돌린 경우 값이 겹치는 현상이 발생함

즉, 스레드가 add()를 실행하는 도중에 다른 스레드가 add()를 호출하여, sum에 대한 충돌이 생김을 알 수 있다.

Comments