ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • synchronized
    StaticPL/JAVA 2020. 2. 27. 00:43

    1. Overview

    Sometimes, you only want to prevent multiple thread access to part of the code inside a method instead of the entire method. The section of code you want to isolate this way is called a critical section and is also created using the synchronized keyword.

    2. Critical section

    It this way, we can really achieve atomicity for any number of distinct operations without worrying about concurrency issues. Luckily for us, the JVM with the support from the operating system and the hardware provides us with multiple tools to guard a critical section against concurrent access by multiple threads I want to point out that the concepts and principles are the same everywhere.

    3. Synchronized

    • Locking mechanism
    • Used to restrict access to a critical section or entire method to a single thread at a time
    • synchronized block is reentrant
    • A thread cannot prevent itself from entering a critical section

    3.1 synchronized on a method

    When multiple threads are going to try to call these methods on the same object of this class only one thread would be able to execute either of those methods notice that if thread a is

    3.2 synchronized on an object

    4. Example

    4.1 Case 1

    public static void main(String [] args) {
            SharedClass sharedObject = new SharedClass();
     
            Thread thread1 = new Thread(() -> {
                while (true) {
                    sharedObject.increment();
                }
            });
     
            Thread thread2 = new Thread(() -> {
                while (true) {
                    sharedObject.decrement();
                }
            });
     
            thread1.start();
            thread2.start();
        }
     
        private static class SharedClass {
            private int counter = 0;
     
            public synchronized void increment() {
                this.counter++;
            }
     
            public synchronized void decrement() {
                this.counter--;
            }
        }

    When thread1 is executing sharedObject.increment(), thread2 cannot execute sharedObject.decrement(). And when thread2 is executing sharedObject.decrement(), thread1 cannot execute sharedObject.increment(). That's because both methods are synchronized, and belong to the same object.

    4.2 Case 2

    public static void main(String [] args) {
            SharedClass sharedObject = new SharedClass();
     
            Thread thread1 = new Thread(() -> {
                while (true) {
                    sharedObject.incrementCounter1();
                }
            });
     
            Thread thread2 = new Thread(() -> {
                while (true) {
                    sharedObject.incrementCounter2();
                }
            });
     
            thread1.start();
            thread2.start();
        }
     
        private static class SharedClass {
            private int counter1 = 0;
            private int counter2 = 0;
     
            private Object lock1 = new Object();
            private Object lock2 = new Object();
     
            public void incrementCounter1() {
                synchronized (lock1) {
                    this.counter1++;
                }
            }
     
            public void incrementCounter2() {
                synchronized (lock2) {
                    this.counter2++;
                }
            }
        }

    When thread1 is executing sharedObject.incrementCounter1(), thread2 can execute sharedObject.incrementCounter2(). That's because of the synchronized blocks inside those methods, synchronized on different lock objects.

    5. Reference

    https://www.linuxtopia.org/online_books/programming_books/thinking_in_java/TIJ315_013.htm

    https://www.geeksforgeeks.org/synchronized-in-java/

    https://medium.com/packt-hub/java-multithreading-how-to-synchronize-threads-to-implement-critical-sections-and-avoid-race-b9259c947e2b

    'StaticPL > JAVA' 카테고리의 다른 글

    ReentrantReadWriteLock  (0) 2020.02.27
    ReentrantLock, lockInterruptibly, and tryLock  (0) 2020.02.27
    Atomic Variables  (0) 2020.02.26
    Access Modifier  (0) 2020.02.23
    Is-A and Has-A Relationship in Java  (0) 2020.02.05

    댓글

Designed by Tistory.