In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the concurrency tools commonly used in java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what concurrency tools are commonly used in java.
1) locking CountDownLatch
Function: let a main thread wait for a set of worker threads to finish execution before continuing to execute the main thread, similar to the function of the join () method. Principle: 1) CountDownLatch is realized by a counter, and the initial value of the counter is the number of waiting threads. 2) when a thread completes its task, the value of the counter is reduced by 1. 3) when the counter value is 0, it means that all threads have finished execution, and at this point, the thread waiting on the lock can resume task execution. Note: 1) countDown () can be called once per thread or multiple times per thread. 2) the counter must be greater than or equal to 0. When the counter is 0, the current thread will not be blocked when the await method is called.
2) barrier (fence) CyclicBarrier
Function: allow a group of threads to be blocked when they reach a barrier, the barrier will not trip until the last thread reaches the barrier, and all threads blocked by the barrier will continue to run. The difference between CyclicBarrier and CountDownLatch: 1) counters for CountDownLatch can only be used once, while counters for CyclicBarrier can be reset using the reset () method. 2) after reaching a certain point, the thread using CountDownLatch continues to run, and the thread using CyclicBarrier stops running: after the thread using CountDownLatch runs to a certain point, call the countDown () method to subtract the value of the counter by 1, and then the thread continues to execute. After a thread using CyclicBarrier runs to a certain point, call the await () method to stop the thread from running until all threads reach that point (calling the await method) before the thread can continue to run.
3) semaphore Semaphore
Function: control the number of concurrent threads. Principle: 1) Thread obtains the license through the acquire () method. 2) the thread returns the license through the release () method. Note: you can get the result of execution immediately by using the tryAcquire () method: try to get a license, if it is successful, return true immediately, if it fails, return false immediately. Application: flow control, such as the control of the number of database connections.
4) Exchange Exchanger
Function: data exchange between two threads. Principle: 1) Thread A calls the public V exchange (V dataA) method, and Thread A reaches the synchronization point and waits until thread B reaches the synchronization point. 2) Thread B calls the public V exchange (V dataB) method, and Thread B reaches the synchronization point. 3) when both thread An and thread B reach the synchronization point, the thread passes its own data to each other, and the two threads exchange the data. For example: public class ExchangerTest {public static void main (String [] args) {final Exchanger exchanger = new Exchanger (); ExecutorService service = Executors.newFixedThreadPool (2) Service.execute (new Runnable () {public void run () {try {String originalData = "dataA"; Thread.sleep (2000) System.out.println ("thread" + Thread.currentThread (). GetName () + "is passing data [" + originalData + "]..." + "time:" + System.currentTimeMillis ()); String exchangedData = exchanger.exchange (originalData) System.out.println ("thread" + Thread.currentThread () .getName () + "get data from another thread [" + exchangedData + "]" + "time:" + System.currentTimeMillis ()) } catch (Exception e) {e.printStackTrace ();}}) Service.execute (new Runnable () {public void run () {try {String originalData = "dataB"; Thread.sleep (6000) System.out.println ("thread" + Thread.currentThread (). GetName () + "is passing data [" + originalData + "]..." + "time:" + System.currentTimeMillis ()); String exchangedData = exchanger.exchange (originalData) System.out.println ("thread" + Thread.currentThread () .getName () + "get data from another thread [" + exchangedData + "]" + "time:" + System.currentTimeMillis ()) } catch (Exception e) {e.printStackTrace ();}}) Result: thread pool-1-thread-1 is passing data [dataA] out. The time:1506414528645 thread pool-1-thread-2 is passing data [dataB] out. Time:1506414532645 thread pool-1-thread-2 gets the data of another thread [dataA] time:1506414532645 thread pool-1-thread-1 gets the data of another thread [dataB] time:1506414532645 Thank you for your reading, these are the contents of "what are the concurrency tools commonly used in java", after the study of this article I believe that we have a deeper understanding of the concurrency tools commonly used in java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.