Learning Java, use of synchronized keyword

前端 未结 2 853
既然无缘
既然无缘 2020-12-03 06:45

so i was testing with synchronized keyword. Here is an example that I tried:

public class MyTest {
    static int i = 0;
    public static voi         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 07:25

    Two things:

    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.

    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. (An important exception: final fields, which cannot be modified after the object is constructed, can be safely read through non-synchronized methods, once the object is constructed).

    source: http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html

提交回复
热议问题