In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what are the concurrent keywords in Java, which can be used for reference by interested friends. I hope you can learn a lot after reading this article. Let's take a look at it.
The nature of concurrency
1. Visibility
A. ensure that any thread modification variable is immediately written to the main memory (heap)
B. all thread references to the variable will be forced to invalidate immediately, and the thread must go to the heap to get the latest variable value.
2. Instruction reordering
The execution order of the code is in JVM (compiler optimizes instruction-level parallel reordering memory system to reorder the final sequence of instructions executed)
3. Atomicity
At most one thread can access the code at a time.
Volatile-> visibility, instruction reordering
Synchronied-> visibility, instruction reordering, atomicity
Lock
Existence value
Synchronized releases the lock only in the following two situations
1) the thread that acquired the lock finished executing the block of code, and then the thread released the possession of the lock
2) if there is an exception in thread execution, JVM will let the thread release the lock automatically.
Therefore, synchronized may cause thread blocking.
To solve the problem of thread blocking, this concept is introduced.
Public interface Lock {
Void lock (); / / acquire the lock and wait if the lock is acquired by another thread
The void lockInterruptibly () throws InterruptedException; / / tryLock () method has a return value, which indicates that it is used to attempt to acquire the lock, true is returned if the acquisition is successful, and false is returned if the acquisition fails (that is, the lock has been acquired by another thread), which means that the method will return immediately anyway. You don't always wait there when you can't get the lock. The boolean tryLock (); / / tryLock (long time, TimeUnit unit) method is similar to the tryLock () method, except that this method waits for a certain amount of time when the lock is not available, and returns false if the lock is not available within the time limit. If you get the lock at first or during the wait, return true. boolean tryLock (long time, TimeUnit unit) throws InterruptedException; void unlock (); / / release lock Condition newCondition ()
}
Final
The variables modified by final are immutable and perform well in concurrent programming scenarios.
The difference between volatile and synchronied
1. Volatile does not cause thread blocking, while synchronized may cause thread blocking. (caused by atomic differences)
2. Volatile can only modify variables, while synchronized can modify variables and methods.
3. When the value of a field depends on its previous value, volatile will not work, such as nymphomorph1 and so on. If the value of a field is limited by the values of other fields, then volatile does not work either, such as the lower and upper boundaries of the Range class, which must follow lower
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.