java synchronized

By | 2021년 11월 28일
Table of Content

java synchronized

참조

정의

Multi Thread 상황에서 특정 데이타의 동기화를 보장하는 키워드이다.

데이타의 안정성과 신뢰성을 보장하는 키워드이지만,
하나의 Thread 만 데이타에 접근 가능하고, 다른 Thread 는 데이타에 접근할 수 없다.

즉, 데이타에 접근하는 다른 모든 Thread 는 실행이 멈추게 된다.

사용법

메서드에서 사용

하나의 Thread 만 메소드를 호출할 수 있다.

public class TestThread {

    public static void main(String[] args) {

        TestThread thread = new TestThread();

        System.out.println("Test start!");

        new Thread(() -> {
            for (int i = 1; i <= 5; i++) {
                thread.call("Thread A");
            }
        }).start();

        new Thread(() -> {
            for (int i = 1; i <= 5; i++) {
                thread.call("Thread B");
            }
        }).start();

        System.out.println("Test end!");
    }

    public synchronized void call(String who) {

        System.out.println(who + " started.");
        try {
            System.out.println(who);
            long sleep = (long) (Math.random() * 100);
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(who + " ended.");
    }
}
Test start!
Test end!
Thread A started.
Thread A
Thread A ended.
Thread B started.
Thread B
Thread B ended.
Thread A started.
Thread A
Thread A ended.
Thread B started.
Thread B
Thread B ended.
Thread A started.
Thread A
Thread A ended.
Thread B started.
Thread B
Thread B ended.
Thread B started.
Thread B
Thread B ended.
Thread A started.
Thread A
Thread A ended.
Thread B started.
Thread B
Thread B ended.
Thread A started.
Thread A
Thread A ended.

변수에 사용

public class TestThread {

    final Integer COUNT = 0;

    public static void main(String[] args) {

        TestThread thread = new TestThread();

        System.out.println("Test start!");

        new Thread(() -> {
            for (int i = 1; i <= 5; i++) {
                thread.call("Thread A");
            }
        }).start();

        new Thread(() -> {
            for (int i = 1; i <= 5; i++) {
                thread.call("Thread B");
            }
        }).start();

        System.out.println("Test end!");
    }

    public void call(String who) {

        System.out.println(who + " started.");
        try {
            synchronized(COUNT) {
                System.out.println(who + " COUNT started.");
                System.out.println(who);
                long sleep = (long) (Math.random() * 100);
                Thread.sleep(sleep);
                System.out.println(who + " COUNT ended.");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(who + " ended.");
    }
}
Test start!
Test end!
Thread A started.
Thread B started.
Thread A COUNT started.
Thread A
Thread A COUNT ended.
Thread B COUNT started.
Thread B
Thread A ended.
Thread A started.
Thread B COUNT ended.
Thread B ended.
Thread B started.
Thread B COUNT started.
Thread B
Thread B COUNT ended.
Thread A COUNT started.
Thread A
Thread B ended.
Thread B started.
Thread A COUNT ended.
Thread B COUNT started.
Thread B
Thread A ended.
Thread A started.
Thread B COUNT ended.
Thread A COUNT started.
Thread A
Thread B ended.
Thread B started.
Thread A COUNT ended.
Thread B COUNT started.
Thread B
Thread A ended.
Thread A started.
Thread B COUNT ended.
Thread A COUNT started.
Thread A
Thread B ended.
Thread B started.
Thread A COUNT ended.
Thread B COUNT started.
Thread B
Thread A ended.
Thread A started.
Thread B COUNT ended.
Thread B ended.
Thread A COUNT started.
Thread A
Thread A COUNT ended.
Thread A ended.

답글 남기기