What does 'synchronized' mean?

后端 未结 17 1624
广开言路
广开言路 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:20

    synchronized is a keyword in Java which is used to make happens before relationship in multithreading environment to avoid memory inconsistency and thread interference error.

    0 讨论(0)
  • 2020-11-22 00:21

    What is the synchronized keyword?

    Threads communicate primarily by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors. The tool needed to prevent these errors is synchronization.

    Synchronized blocks or methods prevents thread interference and make sure that data is consistent. At any point of time, only one thread can access a synchronized block or method (critical section) by acquiring a lock. Other thread(s) will wait for release of lock to access critical section.

    When are methods synchronized?

    Methods are synchronized when you add synchronized to method definition or declaration. You can also synchronize a particular block of code with-in a method.

    What does it mean pro grammatically and logically?

    It means that only one thread can access critical section by acquiring a lock. Unless this thread release this lock, all other thread(s) will have to wait to acquire a lock. They don't have access to enter critical section with out acquiring lock.

    This can't be done with a magic. It's programmer responsibility to identify critical section(s) in application and guard it accordingly. Java provides a framework to guard your application, but where and what all sections to be guarded is the responsibility of programmer.

    More details from java documentation page

    Intrinsic Locks and Synchronization:

    Synchronization is built around an internal entity known as the intrinsic lock or monitor lock. Intrinsic locks play a role in both aspects of synchronization: enforcing exclusive access to an object's state and establishing happens-before relationships that are essential to visibility.

    Every object has an intrinsic lock associated with it. By convention, a thread that needs exclusive and consistent access to an object's fields has to acquire the object's intrinsic lock before accessing them, and then release the intrinsic lock when it's done with them.

    A thread is said to own the intrinsic lock between the time it has acquired the lock and released the lock. As long as a thread owns an intrinsic lock, no other thread can acquire the same lock. The other thread will block when it attempts to acquire the lock.

    When a thread releases an intrinsic lock, a happens-before relationship is established between that action and any subsequent acquisition of the same lock.

    Making methods synchronized has two effects:

    First, it is not possible for two invocations of synchronized methods on the same object to interleave.

    When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

    Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object.

    This guarantees that changes to the state of the object are visible to all threads.

    Look for other alternatives to synchronization in :

    Avoid synchronized(this) in Java?

    0 讨论(0)
  • 2020-11-22 00:21

    To my understanding synchronized basically means that the compiler write a monitor.enter and monitor.exit around your method. As such it may be thread safe depending on how it is used (what I mean is you can write an object with synchronized methods that isn't threadsafe depending on what your class does).

    0 讨论(0)
  • 2020-11-22 00:22

    synchronized simple means no two threads can access the block/method simultaneously. When we say any block/method of a class is synchronized it means only one thread can access them at a time. Internally the thread which tries to access it first take a lock on that object and as long as this lock is not available no other thread can access any of the synchronized methods/blocks of that instance of the class.

    Note another thread can access a method of the same object which is not defined to be synchronized. A thread can release the lock by calling

    Object.wait()
    
    0 讨论(0)
  • 2020-11-22 00:28

    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:

    • Concurrency
    • Java Memory Model

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

    0 讨论(0)
  • 2020-11-22 00:28

    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.

    0 讨论(0)
提交回复
热议问题