Thread synchronization, Object level lock, class level lock
Synchronization is generally referred to in multi-threading, where a synchronous block code can be executed only in one thread at a time.
Java supports multi-threaded execution, so there may be two or more threads accessing the same field or object. Synchronization is a process that keeps all concurrent threads executing synchronously. Synchronization avoids memory consistency errors caused by inconsistent shared memory issues. When a method is declared as synchronous and there is a thread holding the monitor of the method object, your thread will be blocked until the thread releases the monitor.
Synchronization is implemented in Java using this keyword, you can use this keyword in the method or block of code that defines the class, but the keyword cannot be used in variables or attributes defined by the class.
Object Level Lock
An object-level lock is a mechanism that, when you want to synchronize a non-static method or a non-static block of code, allows only one thread to execute this block of code in a given class instance, which can make the instance-level data thread-safe. The following are the specific practices:
public class DemoClass
{public
synchronized void DemoMethod () {}
}
or public
class DemoClass
{Public
void DemoMethod () {
synchronized (this)
{
//other thread safe Code
}
}
}
or public
class DemoClass
{
Private Final object lock = new Object ();
public void DemoMethod () {
synchronized (lock)
{
//other thread safe Code
}
}
}
Class-Level locks
In all mutable instances or running environments, class-level locks prevent multithreading from entering a synchronization block, that is, if there are 100 instances of Democlass in the running environment, there can be only one instance of Democlass executing its demomethod () method at any given time. All other Democlass instances can only be blocked in other threads, which makes the static data thread-safe.
public class DemoClass
{public
synchronized static void DemoMethod () {}
}
or public
class DemoClass
{public
void DemoMethod () {
synchronized (democlass.class)
{
//other thread safe code
}
}
}
or public
class DemoClass
{
Private final static object lock = new Object ();
public void DemoMethod () {
synchronized (lock)
{
//other thread safe Code
}
}
}
Some important points
1, in Java, synchronization ensures that two threads cannot concurrently perform the same synchronous method that requires the same synchronization or concurrent lock.
2, synchronous keywords can only be used in methods or blocks of code, these methods or blocks of code can make static or non-static.
3, when a thread enters the Java synchronization method or block, it acquires a lock, and whenever it leaves the javasynchronized method or block, it releases the lock. The lock is released when execution finishes leaving the synchronization block or any errors and exceptions occur.
4,java synchronization is a keyword that is inherently reentrant, which means that if the Java synchronization method calls to another synchronization method, and the method requires the same lock, then the method that holds the lock can enter another method that requires the same lock.
For example:
Class MyClass {public
synchronized void Method1 () {
method2 ();
}
Public synchronized void Method2 () {
}
}
The above code two method method 1 Method 2 and all with the synchronous decoration, if a moment, the line Cheng execution to Method 1, this time the line Cheng acquires the lock of this object, and because the method 2 is also synchronous method, if synchronization does not have the reentrant sex, this time the line Cheng need to re-apply for the lock. This creates a problem, however, because the thread Cheng has already held the lock on the object and is requesting a lock on the object, so that the line Cheng waits for a lock that will never be acquired.
This behavior does not occur because both the synchronization and the lock have reentrant features.
5, if the object used in the synchronization block is empty, then the Java synchronization will throw a null pointer exception.
For example, a null pointer exception is thrown below
public class DemoClass
{
private final static Object lock = null;
public void DemoMethod () {
synchronized (lock)
{
}
}
}
6, in Java, the synchronization of the method will affect the performance of the application, so when fully required to use this synchronization, in addition, you can consider using synchronous code block to synchronize the key code you need to synchronize.
7, the static and non-static synchronization methods are likely to run concurrently because they use different object locks.
8, according to the Java language Specification, cannot be used in the constructor of the Java synchronization keyword, it is illegal and can cause compilation errors.
9, in Java, non-final fields cannot be synchronized in the synchronization block, because references to non-final fields may change at any time, so that if different threads synchronize unused objects, they are equivalent to no synchronization. It is best to use a string class, which is declared as final and immutable.
Original: Thread synchronization, object-level locking, and class-level locking