In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to carry on the Java synchronization mechanism, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Java's support and synchronization mechanism for multithreading is very popular. It seems that the problem of multithreaded shared data synchronization can be easily solved by using the synchronized keyword. What's going on? -- We need to have an in-depth understanding of the role of the synchronized keyword before we can come to a conclusion.
Generally speaking, the synchronized keyword can be used as a modifier for a function or as a statement within a function, that is, a synchronization method and a synchronous statement block. If classified in more detail, synchronized can act on instance variables, object reference (object references), static functions, and class literals (class name literal constants).
a. Whether the synchronized keyword is added to a method or an object, the lock it acquires is the object, not the
A piece of code or function acts as a lock-- and synchronization methods are likely to be accessed by objects of other threads as well.
B. only one lock per object is associated with it.
C. the realization of synchronization takes a lot of system overhead as a price, and may even cause deadlock, so try to avoid unnecessary synchronization control.
Then let's discuss the impact of different uses of synchronized on the code:
Suppose P1 and P2 are different objects of the same class, and the following synchronization blocks or synchronization methods are defined in this class, and both P1 and P2 can call them.
1. When synchronized is used as a function modifier, the sample code is as follows:
Public synchronized void methodAAA ()
{
/ / … .
}
This is the synchronization method, so which object is synchronized locking at this time? It locks the call to the synchronous method object. In other words, when an object P1 executes this synchronization method in different threads, they will form mutual exclusion and achieve the effect of synchronization. But another object, P2, generated by the Class to which this object belongs, can call the method with the synchronized keyword added at will.
The sample code above is equivalent to the following code:
Public void methodAAA ()
{
Synchronized (this) / / 1)
{
/ / … ..
}
}
What does the this at (1) mean? It refers to the object that calls this method, such as P1. It can be seen that the essence of the synchronization method is to apply synchronized to object reference. Only the thread that gets the P1 object lock can call the P1 synchronization method. For P2, the P1 lock has nothing to do with it, and the program may get rid of the control of the synchronization mechanism in this case, causing data confusion:
2. Synchronization block. The sample code is as follows:
Public void method3 (SomeObject so)
{
Synchronized (so)
{
/ / … ..
}
}
At this point, the lock is the so object, and whoever gets the lock can run the code it controls. You can write a program like this when you have an explicit object as a lock, but when you don't have an explicit object as a lock and you just want a piece of code to synchronize, you can create a special instance variable (which has to be an object) to act as a lock:
Class Foo implements Runnable
{
Private byte [] lock = new byte [0]; / / Special instance variable
Public void methodA ()
{
Synchronized (lock) {/ / … }
}
/ / … ..
}
Note: a zero-length byte array object will be more economical to create than any object-- look at the compiled bytecode: it only takes three opcodes to generate a zero-length byte [] object, while Object lock = new Object () requires seven lines of opcodes.
3. Apply synchronized to the static function. The sample code is as follows:
Class Foo
{
Public synchronized static void methodAAA () / / synchronous static function
{
/ / … .
}
Public void methodBBB ()
{
Synchronized (Foo.class) / / class literal (class name literal constant)
}
}
The methodBBB () method in the code uses class literal as a lock, and it has the same effect as the synchronous static function. The lock obtained is very special, which is the class to which the object currently calling the method belongs (Class, rather than a specific object generated by this Class).
I remember seeing in the book "Effective Java" that it is not the same to use Foo.class and P1.getClass () as synchronous locks. You can't use P1.getClass () to lock this Class. P1 refers to the object produced by the Foo class.
It can be inferred that if a class defines a static function An of synchronized and an instance function B of synchronized, then the same object of this class, Obj, will not be synchronized when accessing An and B methods in multithreading, because their locks are different. The lock of method B is the object Obj, and the lock of An is the Class to which Obj belongs.
Undefined
The summary is as follows:
Figuring out which object synchronized is locking can help us design safer multithreaded programs.
There are also some tips to make our synchronous access to shared resources more secure:
1. Define the instance variable of private + its get method, instead of defining the instance variable of public/protected. If the variable is defined as public, the object can bypass the control of the synchronization method and directly take it and change it. This is also one of the standard implementations of JavaBean.
2. If the instance variable is a mutable object, special array or ArrayList, then the above method is still not safe, because when the external object gets the reference to the instance object through the get method and points it to another object, then the private variable will also change, isn't it very dangerous.
At this point, you need to synchronize the get method with synchronized, and only return the clone () of the private object-- so that the caller gets a reference to the copy of the object.
Alternatively, you can return a copy reference (local variable) of the instance variable.
This is the answer to the question about how to carry out Java synchronization mechanism. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.