What does 'synchronized' mean?

后端 未结 17 1880
广开言路
广开言路 2020-11-22 00:13

I have some questions regarding the usage and significance of the synchronized keyword.

  • What is the significance of the synchronized
17条回答
  •  孤城傲影
    2020-11-22 00:42

    Synchronized simply means that multiple threads if associated with single object can prevent dirty read and write if synchronized block is used on particular object. To give you more clarity , lets take an example :

    class MyRunnable implements Runnable {
        int var = 10;
        @Override
        public void run() {
            call();
        }
    
        public void call() {
            synchronized (this) {
                for (int i = 0; i < 4; i++) {
                    var++;
                    System.out.println("Current Thread " + Thread.currentThread().getName() + " var value "+var);
                }
            }
        }
    }
    
    public class MutlipleThreadsRunnable {
        public static void main(String[] args) {
            MyRunnable runnable1 = new MyRunnable();
            MyRunnable runnable2 = new MyRunnable();
            Thread t1 = new Thread(runnable1);
            t1.setName("Thread -1");
            Thread t2 = new Thread(runnable2);
            t2.setName("Thread -2");
            Thread t3 = new Thread(runnable1);
            t3.setName("Thread -3");
            t1.start();
            t2.start();
            t3.start();
        }
    }
    

    We've created two MyRunnable class objects , runnable1 being shared with thread 1 and thread 3 & runnable2 being shared with thread 2 only. Now when t1 and t3 starts without synchronized being used , PFB output which suggest that both threads 1 and 3 simultaneously affecting var value where for thread 2 , var has its own memory.

    Without Synchronized keyword
    
        Current Thread Thread -1 var value 11
        Current Thread Thread -2 var value 11
        Current Thread Thread -2 var value 12
        Current Thread Thread -2 var value 13
        Current Thread Thread -2 var value 14
        Current Thread Thread -1 var value 12
        Current Thread Thread -3 var value 13
        Current Thread Thread -3 var value 15
        Current Thread Thread -1 var value 14
        Current Thread Thread -1 var value 17
        Current Thread Thread -3 var value 16
        Current Thread Thread -3 var value 18
    

    Using Synchronzied, thread 3 waiting for thread 1 to complete in all scenarios. There are two locks acquired , one on runnable1 shared by thread 1 and thread 3 and another on runnable2 shared by thread 2 only.

    Current Thread Thread -1 var value 11
    Current Thread Thread -2 var value 11
    Current Thread Thread -1 var value 12
    Current Thread Thread -2 var value 12
    Current Thread Thread -1 var value 13
    Current Thread Thread -2 var value 13
    Current Thread Thread -1 var value 14
    Current Thread Thread -2 var value 14
    Current Thread Thread -3 var value 15
    Current Thread Thread -3 var value 16
    Current Thread Thread -3 var value 17
    Current Thread Thread -3 var value 18
    

提交回复
热议问题