I. Thread Synchronization and lock in java
What is a lock? For example, in real life, every door has a lock, and the entrance to each synchronized code block has a "Door", which is locked, to do what you want, you must obtain the lock key. In java, each object has only one lock. The basic data type is unlocked and cannot be used for synchronization.
Ii. synchronized keywords in java
The synchronized keyword is used to identify the lock required by the code block to be executed by the thread. Synchronized can be used for method modification or code block. In this document, synchronized serves as a code block.
1. When synchronized modifies a non-static method, the synchronization method is synchronized on the object (that is, this) of the current instance of the class, that is, you must obtain the lock of the current instance object to execute this method. When a thread enters the synchronized Method of the class or the code block synchronized on this, because all synchronized methods of the class are synchronized on this, therefore, in addition to the methods currently accessed by this thread, other threads cannot access other synchronized methods or code blocks synchronized on this class. It should be noted that the access is not allowed here, but only within the data range of the current instance.
For example:
Public class Test1 {public synchronized void f () {// method body }}
Because synchronized is essentially a synchronous code block, the above Code is equivalent:
Public void f () {synchronized (this) {// method body }}
2. When synchronized modifies the static method, it is synchronized throughout the Class (that is, the Class Object of the Class). That is to say, when a thread accesses a thread marked as synchronized, other threads cannot access any synchronized static method of all instances of this class. See the following code:
package com.litl.java.test;import java.util.concurrent.TimeUnit;public class Test1 {public synchronized static void f(){System.out.println("f-start---------------");try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f_end>>>>>>>>>>>>>>>>");}public synchronized void g() {System.out.println("g-start-----------");try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("g_end>>>>>>>>>>>>>");}public static void main(String[] args) {for(int i=0;i<10;i++){final Test1 t=new Test1();new Thread(new Runnable() {@Overridepublic void run() {t.f();t.g();}}).start();}}}
Print result:
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
F-start ---------------
G-start -----------
F_end >>>>>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
G_end >>>>>>>>>>>>>>
F-start ---------------
G-start -----------
G_end >>>>>>>>>>>>>>
F_end >>>>>>>>>>>>>>>>>
G-start -----------
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
F-start ---------------
G_end >>>>>>>>>>>>>>
F_end >>>>>>>>>>>>>>>>>
G-start -----------
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
G_end >>>>>>>>>>>>>>
F-start ---------------
G-start -----------
F_end >>>>>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
G_end >>>>>>>>>>>>>>
We can see that the f () method is executed synchronously, while the g () method is executed alternately.
Analysis:
Because f () is a static method, and synchronized is identified as it is synchronized on the Class, while g () is not a static method, it is synchronized on this, the two locks are different. Because the main method executes the f () and g () methods for multiple class instances, f () Synchronization plays a role here, the synchronization to g () is invalid.
If you want to synchronize g (), you can modify the code to synchronize g () on the Class:
public void g() {synchronized(Test1.class){System.out.println("g-start-----------");try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("g_end>>>>>>>>>>>>>");}}
Print result:
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
F-start ---------------
F_end >>>>>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
G-start -----------
G_end >>>>>>>>>>>>>>
Iii. Summary
1. Each object has a lock. The basic data type cannot be used for synchronization.
2. When talking about synchronization, we must first determine the object on which the synchronization will take effect. Only when two threads are synchronizing on the same object
3. The Class lock and the Class instance lock are different. When static is locked, if the non-static method is not synchronized on the Class, other threads that do not obtain the Class lock can still access this method.