Chapter II Concurrent access to objects and variables
This section summarizes some of the synchronized usage methods:
In Java: "Non-thread-safe" issues exist in "instance variables", and if they are private variables within a method, there is no "thread-safe" issue.
synchronized is added to the front of the method:
Synchronized acquired locks are object locks, rather than a piece of code or method as a lock, during the execution of threads, which thread executes the method with the Synchronized keyword, which thread holds the lock of the object that the method belongs to, then other threads can only wait, The premise is that multiple threads are accessing the synchronized method of the same object. If multiple threads access multiple objects, the JVM creates multiple locks.
If a thread already holds a lock on an object, another thread cannot access the object's synchronization method, but it can access the object's unsynchronized methods.
Synchronization does not have inheritance, if a method of the parent class owns the Synchronized keyword, the method of subclasses must join the SYNCHRONIZED keyword manually if it wants to synchronize;
synchronized Sync code block synchronized (this): a thread that accesses code in a synchronized code block cannot access any of the synchronized code blocks of that object, but it can access the asynchronous code block. The synchronized code block is also locked to the current object.
to use any object as a monitor: synchronized (non-this object)
With multiple threads holding a pair like a monitor as the same object, only one thread at a time can execute a synchronized code block in the synchronized (not the This object)
While holding the object monitor as the same object, only one thread at a time can execute code in the synchronized (non-this object) synchronized code block.
Three conclusions:
When multiple threads simultaneously perform a synchronized (x) {} Synchronization code block time synchronization effect.
Synchronization occurs when other threads perform the synchronized synchronization method in the X object.
Synchronization effect when other threads execute the synchronized (this) code inside the X object method
If another thread calls the Synchronized keyword method, it is called asynchronously.
Static synchronization: There are two kinds of synchronized keyword in front of static method, one is synchronize (class)
keyword added to the static method is to lock class, add to non static is to lock the object.
Class locks can work on all instances of a class, that is, at any time, only one thread can access the static method of the class. Because the static method is the class method.
About the case where the lock is a string:
Because of the existence of a string constant pool in Java, str1 = "AA" in the program, and str1 = "AA" is actually pointing to a form object, so when the lock is applied synchronized (STR1) and synchronized (STR2) The same lock is requested. Therefore, in most cases, the synchronized synchronized code block does not use string as the lock object. Switch to something else.
To avoid long waiting for threads, use synchronized to synchronize code blocks as much as possible, with less synchronized methods.
when any data type is used as a synchronization lock, it should be noted that there are multiple threads holding the lock object at the same time, and if the same lock object is held simultaneously, the threads are synchronized and, if the lock object is acquired separately, the threads are not synchronized.