Multi-Threaded synchronization: Multiple threads work together to perform tasks correctly.
We often see the multithread synchronization keyword synchronized, then it is the use of Who do sync lock.
To achieve multithreaded synchronization, you must ensure that the sync lock is shared only.
Synchronized is typically used in 3 scenarios:
1. Non-static methods
2. Static method
3. Code block
First, non-static methods
A synchronization lock is an instance of the object in which the current method resides, and if the object is a singleton, thread synchronization can be implemented.
Public synchronized void Run () {for
(int start =1; start < start++) {
System.out.println ("No." + Threadno + ":" + start);
}
Second, static method
Synchronous locks are the classes in which the current method is located, and the class is unique, so it is certain that thread synchronization can be implemented.
public static synchronized void Test () {
System.out.println ("test");
}
Three, code block
Synchronization locks can be customized, but are guaranteed to be unique.
public void Run () {synchronized (.) {for
(int start = 1; start < start++) {
System.out.println ("No . "+ Threadno +": "+ Start";}}}
Here, this is the instance of the class in which the current method is located, and if it is a single case, it is unique.