This article introduces some knowledge points about synchronized keywords in JAVA multithreading as object locks.
The so-called object lock means synchronized to lock a certain object. For reference, please refer to: This article
1. Analysis
Synchronized can modify the instance method as follows:
public class MyObject { synchronized public void methodA() { //do something.... }Here, the synchronized keyword locks the current object. This is also called object lock.
Why lock the current object? Because methodA() is an instance method, if you want to execute methodA(), you need to call it in the form of object. method() (obj.methodA(), obj is an object of the MyObject class, synchronized means locking the obj object).
The above code can also be written like this:
public class MyObject { public void methodA() { synchronized(this){ //do something.... } }2. Features
An obvious feature of synchronized keyword synchronization is that when multiple synchronized modified instance methods are defined in the MyObject class, if multiple threads have the same object of the MyObject class, these methods can only be executed in a synchronous manner. That is, after executing a synchronized modification method, another synchronized modification method can be executed.
as follows:
public class MyObject { synchronized public void methodA() { //do something.... } synchronized public void methodB() { //do some other thing }}There are two synchronized modification methods in the MyObject class.
public class ThreadA extends Thread { private MyObject object;//Omit the constructor @Override public void run() { super.run(); object.methodA(); }}Thread A executes methodA()
public class ThreadB extends Thread { private MyObject object;//Omit the constructor @Override public void run() { super.run(); object.methodB(); }} Thread B executes methodB()
public class Run { public static void main(String[] args) { MyObject object = new MyObject(); //Thread A and thread B hold the same object: object ThreadA a = new ThreadA(object); ThreadB b = new ThreadB(object); a.start(); b.start(); }}Since thread A and thread B hold the object object of the same MyObject class, although these two threads need to call different methods, they must be synchronized. For example, thread B needs to wait for thread A to execute the methodA() method before it can execute the methodB() method.
3. Conclusion
As can be seen from the above, the scope of the synchronized lock described in this article is the entire object. If there are multiple synchronized modified synchronization methods in a class, and multiple threads hold the same object of the class (the same object of the class), although they call different methods, the execution of each method is also synchronized.
If there are no shared variables between each synchronized method, or there is no connection between each method, but it can only be executed synchronously, this will affect efficiency.
4. Application--Use synchronized to avoid reading dirty data due to data inconsistency
The following example:
public class MyObject { private String userName = "b"; private String password = "bb"; synchronized public void methodA(String userName, String password) { this.userName = userName; try{ Thread.sleep(5000); }catch(InterruptedException e){ } this.passWord = password; } synchronized public void methodB() { System.out.println("userName" + userName + ": " + "passWord" + password); }}methodA() is responsible for changing the username and password. In reality, a username corresponds to a password.
methodB() is responsible for reading the username and password.
If methodB() is not modified with synchronized, thread A changes the username when calling methodA() to line 7, and gives up the CPU for some reason (such as sleeping on line 9).
At this time, if thread B executes methodB(), the username read is the username changed by thread A ("a"), but the password is the original password ("bb"). Because Thread A is sleeping and has not had time to change the password.
However, if methodB() is modified with synchronized, then thread B can only wait for thread A to complete execution (that is, it has changed the user name and password) before executing methodB to read the user name and password. Therefore, dirty reading problems caused by inconsistency in data are avoided.
The above is all about this article, I hope it will be helpful for everyone to learn Java programming.