Java multithreading - Synchronous blocks
Java synchronized block is used to mark the method or code block that is synchronized. Java synchronization blocks are used to avoid competition. This article introduces the following content:
Java synchronized keywords (synchronized)
Synchronized blocks in Java are marked synchronized. Synchronization blocks are synchronized on an object in Java. All synchronization blocks synchronized on an object can only be entered by one thread and perform operations at the same time. All other threads waiting to enter the synchronization block will be blocked until the threads in the synchronization block are executed exit.
There are four different synchronization blocks:
The above synchronization blocks are all synchronized on different objects. Which synchronization block is actually needed depends on the specific situation.
Instance method synchronization
Here is a synchronized example method:
public synchronized void add(int value){this.count += value; }
Static method synchronization
Static method synchronization is the same as instance method synchronization method, and the synchronized keyword is also used. Java static method synchronization is as follows:
public static synchronized void add(int value){ count += value; }Similarly, the synchronized keyword here tells Java that this method is synchronized.
Synchronization of static methods refers to synchronization on the class object where the method is located. Because a class can only correspond to one class object in a Java virtual machine, only one thread is allowed to execute static synchronization methods in the same class.
For static synchronization methods in different classes, a thread can execute static synchronization methods in each class without waiting. Regardless of the static synchronization method in the class being called, a class can only be executed by one thread at the same time.
Synchronous blocks in instance methods
Sometimes you don't need to sync the entire method, but instead synchronize part of the method. Java can synchronize part of a method.
An example of a synchronization block in an asynchronous Java method is shown below:
public void add(int value){ synchronized(this){ this.count += value; } }Examples use the Java synchronous block constructor to mark a piece of code that is synchronized. This code is the same as the synchronization method when executed.
Note that the Java synchronous block constructor encloses the object in brackets. In the above example, "this" is used, that is, the instance itself that calls the add method. Objects enclosed in brackets in the synchronization constructor are called monitor objects. The above code uses monitor object synchronization, and the synchronous instance method uses the instance of the calling method itself as the monitor object.
Only one thread can execute in Java methods synchronized to the same monitor object at a time.
The following two examples synchronize the instance object they call, so they are equivalent in synchronized execution effect.
public class MyClass { public synchronized void log1(String msg1, String msg2){ log.writeln(msg1); log.writeln(msg2); } public void log2(String msg1, String msg2){ synchronized(this){ log.writeln(msg1); log.writeln(msg2); } } }In the above example, only one thread can be executed in any of the two synchronization blocks at a time.
If the second synchronization block is not synchronized on this instance object, the two methods can be executed simultaneously by the thread.
Synchronous blocks in static methods
Similar to the above, the following are examples of two static methods synchronization. These methods are synchronized on the class object to which the method belongs.
public class MyClass { public static synchronized void log1(String msg1, String msg2){ log.writeln(msg1); log.writeln(msg2); } public static void log2(String msg1, String msg2){ synchronized(MyClass.class){ log.writeln(msg1); log.writeln(msg2); } } }These two methods do not allow access by threads at the same time.
If the second synchronization block is not synchronized on the MyClass.class object. Then these two methods can be accessed by threads at the same time.
Java Synchronization Instance
In the following example, two threads are started, both calling the add method of the same instance of the Counter class. Because synchronization is on the instance to which the method belongs, only one thread can access the method at the same time.
public class Counter{ long count = 0; public synchronized void add(long value){ this.count += value; } } public class CounterThread extends Thread{ protected Counter counter = null; public CounterThread(Counter counter){ this.count = counter; } public void run() { for(int i=0; i<10; i++){ counter.add(i); } } } public class Example { public static void main(String[] args){ Counter counter = new Counter(); Thread threadA = new CounterThread(counter); Thread threadB = new CounterThread(counter); threadA.start(); threadB.start(); } }Two threads were created. Their constructor refers to the same Counter instance. The Counter.add method is synchronized on an instance because the add method is an instance method and is marked with the synchronized keyword. Therefore, only one thread is allowed to call the method at a time. Another thread must wait until the first thread exits the add() method before continuing to execute the method.
If two threads refer to two different Counter instances, they can call the add() method at the same time. These methods call different objects, so these methods are synchronized on different objects. These method calls will not be blocked. As shown in the following example:
public class Example { public static void main(String[] args){ Counter counterA = new Counter(); Counter counterB = new Counter(); Thread threadA = new CounterThread(counterA); Thread threadB = new CounterThread(counterB); threadA.start(); threadB.start(); } }Note that these two threads, threadA and threadB, no longer refer to the same counter instance. The add methods of CounterA and counterB are synchronized on the object to which they belong. Calling counterA's add method will not block the call to counterB's add method.
The above is an explanation of the knowledge of Java multi-threaded synchronization blocks. We will continue to add relevant information in the future. Thank you for your support for this site!