In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is the relationship between Lock and Synchronizer, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Synchronizer, which is an object that adjusts thread execution according to its own state. Is the object used to coordinate the execution of (multiple) threads. From this point of view, Java's built-in lock is a synchronizer that coordinates / controls thread execution in a mutually exclusive, reentrant manner.
Java's blocking queue is also a kind of synchronizser.
Another example: semaphore Semaphore, lock Latch, level Barrier are different types of synchronizer.
Semaphore
Semaphore can be used to control the number of threads accessing a resource at the same time. Wrap up the acquisition operation of these resources, call the semaphore acquire () to apply for permission before obtaining the resource, and release the license through release () after the use of the resource. The management of pooled resources can generally be done with semaphores.
Lock up
The English Latch means a latch, which is used to keep the door shut and not to let in or out. The same is true in concurrent programming. We use latch as an object to disable thread execution. When will threads be allowed to pass through this latch? For CountDownLatch, that's when the count changes to zero. For FutureTask, that's when you can get the results of the calculation (or it could be an exception, of course). An application scenario: you can think of a lock as a starting gun, which allows threads to wait for the signal to run together.
Checkpoint
Barrier means "barrier, fence" in Chinese, and the translation of "barrier" means "barrier". It is actually very much like a lock, unlike a lock: a lock waits for events, while levels wait for threads. In terms of the feel of API calls, Barrier calls barrier.await () to block the worker thread after all the serious work of the worker thread has been executed (depending on the business scenario), until all other worker threads complete their own tasks and call barrier.await (), and the level is broken. The condition here is that there are enough threads to call barrier.await (). Latch calls latch.await () in the worker thread, waiting for the lock to be opened to execute. How is the lock opened? It relies on latch.countDown () to be turned on after 0, and whose countdown is not directly related to which thread. Barrier.await () requires real thread blocking, which is why barrier is waiting for threads, latch and so on are signals.
Often use the level to the scenario is that after a task is divided into n sub-tasks, wait for the n sub-tasks to be completed before doing the next step.
The constructor of CyclicBarrier accepts a number of threads and an action to be performed after a level is broken.
Exchanger is another level that is used to exchange data for two threads, of course, in a thread-safe manner.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.