In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use the synchronized keyword in Java. I hope you will get something after reading this article. Let's discuss it together.
1. The underlying implementation principle of synchronized lock.
JVM implements method synchronization and code block synchronization based on entering and exiting Monitor objects. Code block synchronization is achieved using the monitorenter and monitorexit instructions, the monitorenter instruction is inserted at the beginning of the synchronized code block after compilation, and the monitorexit is inserted at the end of the method and at the exception. Any object has a monitor associated with it, and when and a monitor is held, it will be locked.
According to the requirements of the virtual machine specification, when executing the monitorenter instruction, we must first try to acquire the lock of the object. If the object is not locked, or if the current thread already owns the lock of that object, increase the lock counter by 1; accordingly, the lock counter will be subtracted by 1 when the monitorexit instruction is executed, and when the counter is reduced to 0, the lock will be released. If the acquisition of the object lock fails, the current thread blocks waiting until the object lock is released by another thread.
How can I tell if this object is locked? The MarkWord field in the object header records the lock information for the object.
2. Implement singleton pattern public class Singleton {private volatile static Singleton uniqueInstance; private Singleton () {} public static Singleton getUniqueInstance () {/ / lock class object without instantiation if (uniqueInstance = = null) {/ / lock class object synchronized (Singleton.class) {if (uniqueInstance = = null) uniqueInstance = new Singleton () } return uniqueInstance;} / / public static synchronized Singleton getUniqueInstance () {/ / if (uniqueInstance==null) {/ / uniqueInstance= new Singleton (); / /} / / return uniqueInstance;//}}
First of all, why not use the second way to implement the singleton: regardless of whether the object has been instantiated or not, this synchronization method will be called, which will cause a large number of threads to enter blocking, while double lock checking can be used to return directly when the first judgment is not empty without entering the synchronization code block.
A few key points:
Why should the uniqueInstance attribute be decorated with volatile? The new operation is not an atomic operation, but is divided into three steps (allocating the memory space of the object, initializing the object, and setting the uniqueInstance to point to the memory address that has just been allocated). If you do not use volatile,2 and 3, instruction rearrangement may occur, resulting in external access to an object that has not been initialized.
Why is the constructor private? Prevents objects from being created elsewhere.
Why is uniqueInstance private static? Private allows the outside to access the object only in a specific way, static because the object is accessed in a static method.
Why is the getUniqueInstance () method public and static? Public enables the outside world to access the method to obtain the object, and static enables the program to obtain the object through the class name.
Why use double detection to initialize objects? The first detection is mainly used to determine whether the object has been created, and if so, it returns directly; the second detection is because: multiple threads may find that the object is empty in the first detection and enter the synchronous code block at the same time, but only one thread will grab the lock and create the object, and other threads will block waiting in line for the lock to be released. When the thread that created the object returns, the blocked thread is awakened, and the object is no longer empty, so a second detection is needed to prevent the object from being created multiple times.
3. Using class loading to realize singleton mode (hungry mode).
Public class SingleTon2 {/ * * built-in objects are static and create objects directly to ensure that they are loaded at initialization * / public static SingleTon2 instance = new SingleTon2 (); / * * Constructors are private to prevent objects from being created elsewhere * / private SingleTon2 () {} / * * Public static methods return objects * / public static SingleTon2 getInstance () {return instance }} after reading this article, I believe you have a certain understanding of "how to use the synchronized keyword in Java". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.