Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does Lock have to do with Synchronizer?

2025-01-18 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report