What does 'synchronized' mean? What does 'synchronized' mean? multithreading multithreading

What does 'synchronized' mean?


The synchronized keyword is all about different threads reading and writing to the same variables, objects and resources. This is not a trivial topic in Java, but here is a quote from Sun:

synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods.

In a very, very small nutshell: When you have two threads that are reading and writing to the same 'resource', say a variable named foo, you need to ensure that these threads access the variable in an atomic way. Without the synchronized keyword, your thread 1 may not see the change thread 2 made to foo, or worse, it may only be half changed. This would not be what you logically expect.

Again, this is a non-trivial topic in Java. To learn more, explore topics here on SO and the Interwebs about:

Keep exploring these topics until the name "Brian Goetz" becomes permanently associated with the term "concurrency" in your brain.


Well, I think we had enough of theoretical explanations, so consider this code

public class SOP {    public static void print(String s) {        System.out.println(s+"\n");    }}public class TestThread extends Thread {    String name;    TheDemo theDemo;    public TestThread(String name,TheDemo theDemo) {        this.theDemo = theDemo;        this.name = name;        start();    }    @Override    public void run() {        theDemo.test(name);    }}public class TheDemo {    public synchronized void test(String name) {        for(int i=0;i<10;i++) {            SOP.print(name + " :: "+i);            try{                Thread.sleep(500);            } catch (Exception e) {                SOP.print(e.getMessage());            }        }    }    public static void main(String[] args) {        TheDemo theDemo = new TheDemo();        new TestThread("THREAD 1",theDemo);        new TestThread("THREAD 2",theDemo);        new TestThread("THREAD 3",theDemo);    }}

Note: synchronized blocks the next thread's call to method test() as long as the previous thread's execution is not finished. Threads can access this method one at a time. Without synchronized all threads can access this method simultaneously.

When a thread calls the synchronized method 'test' of the object (here object is an instance of 'TheDemo' class) it acquires the lock of that object, any new thread cannot call ANY synchronized method of the same object as long as previous thread which had acquired the lock does not release the lock.

Similar thing happens when any static synchronized method of the class is called. The thread acquires the lock associated with the class(in this case any non static synchronized method of an instance of that class can be called by any thread because that object level lock is still available). Any other thread will not be able to call any static synchronized method of the class as long as the class level lock is not released by the thread which currently holds the lock.

Output with synchronised

THREAD 1 :: 0THREAD 1 :: 1THREAD 1 :: 2THREAD 1 :: 3THREAD 1 :: 4THREAD 1 :: 5THREAD 1 :: 6THREAD 1 :: 7THREAD 1 :: 8THREAD 1 :: 9THREAD 3 :: 0THREAD 3 :: 1THREAD 3 :: 2THREAD 3 :: 3THREAD 3 :: 4THREAD 3 :: 5THREAD 3 :: 6THREAD 3 :: 7THREAD 3 :: 8THREAD 3 :: 9THREAD 2 :: 0THREAD 2 :: 1THREAD 2 :: 2THREAD 2 :: 3THREAD 2 :: 4THREAD 2 :: 5THREAD 2 :: 6THREAD 2 :: 7THREAD 2 :: 8THREAD 2 :: 9

Output without synchronized

THREAD 1 :: 0THREAD 2 :: 0THREAD 3 :: 0THREAD 1 :: 1THREAD 2 :: 1THREAD 3 :: 1THREAD 1 :: 2THREAD 2 :: 2THREAD 3 :: 2THREAD 1 :: 3THREAD 2 :: 3THREAD 3 :: 3THREAD 1 :: 4THREAD 2 :: 4THREAD 3 :: 4THREAD 1 :: 5THREAD 2 :: 5THREAD 3 :: 5THREAD 1 :: 6THREAD 2 :: 6THREAD 3 :: 6THREAD 1 :: 7THREAD 2 :: 7THREAD 3 :: 7THREAD 1 :: 8THREAD 2 :: 8THREAD 3 :: 8THREAD 1 :: 9THREAD 2 :: 9THREAD 3 :: 9


The synchronized keyword prevents concurrent access to a block of code or object by multiple threads. All the methods of Hashtable are synchronized, so only one thread can execute any of them at a time.

When using non-synchronized constructs like HashMap, you must build thread-safety features in your code to prevent consistency errors.