the Synchronized keyword, which consists of two usages: the Synchronized method and the synchronized block.
1. Synchronized method: Declare the Synchronized method by adding the Synchronized keyword to the method declaration. Such as:
Public synchronized void Accessval (int newval);
The Synchronized method Controls access to class member variables: Each class instance corresponds to a lock, and each synchronized method must obtain a lock on the class instance that invokes the method to execute, otherwise the owning thread is blocked, and once the method executes, the lock is exclusive until the lock is released from the method return , the blocked thread can then get the lock and re-enter the executable state. This mechanism ensures that at the same time for each class instance, at most one of its member functions declared as synchronized is in an executable state (since at most one can obtain the corresponding lock for that class instance), This effectively avoids access violations of class member variables (as long as all methods that may access the class member variable are declared as synchronized).
In Java, not only class instances, each class also corresponds to a lock, so we can also declare the static member function of the class as synchronized to control its access to static member variables of the class.
The flaw of the Synchronized method: Declaring a large method as synchronized will greatly affect efficiency, typically if the method run () of the thread class is declared as synchronized, because it is running throughout the lifetime of the threads, As a result, the call to any synchronized method in this class will never succeed. Of course we can put the code that accesses the class member variable into a specialized method, declare it as synchronized, and use it in the main method to solve the problem, but Java provides us with a better solution, that is, the synchronized block.
2. Synchronized BLOCK: Declare the synchronized block by synchronized keyword. The syntax is as follows:
Synchronized (SyncObject) {
Code that allows access control
}
The synchronized block is a block of code in which the code must obtain an object SyncObject (as previously described, which can be a class instance or Class) to execute, as described in the previous mechanism. Because it can be arbitrary code block, and can arbitrarily specify the locked object, it is more flexible.
Some understanding of synchronized (this) (very meticulous, thanks to the author!) )
My understanding of synchronized (this) and synchronized (object):Myth One: The Synchronized keyword can only be used in a method that implements Runnable or inherits the subclass of the thread class.
Positive solution: If a piece of code (or method) may be accessed by multiple threads at the same time, then the operation of the data modification operation may be due to different threads of operation and inconsistent, using synchronized lock this code, to ensure that only one thread access to this block of code at the same time. That is, the keyword synchronized can be used in any method of the class, even if the class does not implement the Runnable interface or inherit the thread class.
Myth Two: synchronized (this) and synchronized (object) have a completely different scope.
Positive solution: When multiple threads access method A () of the same Class A
And this method a () requires one thread to execute after the execution of another thread
Then this method a () 9 must be added synchronized keyword
or write the synchronized in the Method A () (this//refers to an instance of the current Class A) {}
If you do not add the Synchronized keyword when declaring method a () or do not add synchronized (this) {} in Method A ()
When synchronizing blocks,
Can be within the run method of the thread class
Synchronized (Object/= instance of Class A) {
OBJECT.A ();
}
The purpose of implementing multi-threaded simultaneous access to Class A in the synchronization block
The object itself contains this case.
This refers to the class where the current synchronization block resides, when no other class is required.
Object refers to a class that needs to be called, references another class, and needs to handle multi-threaded concurrent access, and object refers to the referenced class. If no other class is referenced, the class itself that contains the method in which the synchronization block resides is referred to.
Cases:
public class Access class {public void a method () { //synchronized (this) {for (int i = 0; i <; i++) { SYSTEM.OUT.P Rintln (Thread.CurrentThread (). GetName () + "i =" + i); } }} public void B method () { //synchronized (this) { = (int i = 0; i <; i++) { System.out.println (Thread . CurrentThread (). GetName () + "i =" + i); } //} }}
public class instance thread class {public static void main (string[] args) { Access class Object = new Access Class (); thread T1 = new Thread (new threads A (Object)); Thread t2 = new Thread (new threads B (Object)); T1.start (); T2.start (); }}class thread A implements Runnable {Private Access class object; public thread A (Access class object) { //TODO auto-generated constructor stub This.object = object;} @Override public void Run () { //TODO auto-generated method stub Synchronized (object) { object. a method (); } }}
Class Thread B implements Runnable {Private Access class object; public thread B (Access class object) { //TODO auto-generated constructor stub< C12/>this.object = object; } @Override public void Run () { //TODO auto-generated method stub Synchronized (object) { object. b method (); } }}
Java multithreading synchronized methods and methods block synchronized (this) and synchronized (object) understanding