In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how to use the synchronized keyword in java. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Description:
Synchronization in java (synchronized) is based on entering the monitor object and exiting the monitor object, whether explicit or implicit.
Synchronized statement block:
1) (using the javap-c class name) after decompiling the class file, you can see that the entry location and exit location (method end and exception) of the synchronization block are inserted into the monitorenter bytecode instruction and the monitorexit bytecode instruction respectively, so the synchronization code block belongs to display synchronization. 2) when the thread executes the monitorenter instruction, it attempts to acquire the lock of the object.
Synchronized method:
1) JVM distinguishes whether a method is a synchronization method from the ACC_SYNCHRONIZED access flag in the method structure (method_info) in the Class file, so the synchronization method belongs to implicit synchronization. 2) when the method is called, the calling instruction will check whether the ACC_SYNCHRONIZED access flag of the method is set. If so, the execution thread will hold the monitor first, then execute the method, and finally release the monitor when the method is completed. 3) other threads cannot get the monitor during method execution. 4) if an exception is thrown during the execution of a synchronous method and the exception cannot be handled within the method, the monitor held by the synchronous method will be automatically released when the exception is thrown outside the synchronous method. The method_info structure format is as follows (excerpt from the java virtual machine specification): method_info {U2 access_flags; / / the flag U2 name_index; U2 descriptor_index; U2 attributes_count; attribute_info attributes used to define the access rights and basic attributes of the current method [attributes _ attributes] } the value of the access tag (access_flags) in the method_info structure: the tag name value indicates ACC_PUBLIC 0x0001 public, and the method can access the ACC_PRIVATE 0x0002 private from outside the package. Method can only access ACC_PROTECTED 0x0004 protected in this class, method can access ACC_STATIC 0x0008 static in itself and subclass, static method ACC_FINAL 0x0010 final, method cannot be overridden ACC_SYNCHRONIZED 0x0020 synchronized Methods are synchronized by monitor... Memory primitive for acquiring and releasing locks: when a thread acquires a lock, JMM invalidates the corresponding local memory for that thread. As a result, the critical section code protected by monitor must read shared variables from main memory. When a thread releases the lock, JMM flushes the shared variables in the corresponding local memory to the main memory, that is, 1 sets the data in the local memory to invalid, 2 copies the data from the main memory to the local memory, and 3 operates in the local memory. 4 refresh the data in the local memory to the main memory after the operation is completed. The whole looks as if it is operating directly in main memory. Reentrancy of synchronized: 1) when a thread asks again for a critical resource that holds an object lock, this is a reentrant lock and the request will succeed. 2) because synchronized is implemented based on monitor, the counter in monitor will still be incremented by 1 each time you re-enter. After reading the above, do you have any further understanding of how to use the synchronized keyword in java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.