In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "java how to use Semaphore to achieve current limiter", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "java how to use Semaphore to achieve current limiter" bar!
Concept
1. Semaphore can be seen as a semaphore that has been widely translated into semaphores. Conceptually, semaphores maintain a set of credentials, and the thread that obtains the credentials can access resources. After using them, we can release them. We can use semaphores to restrict concurrent threads accessing specific resources.
2, can be simply summarized as: a counter, a waiting queue, three methods. In the semaphore model, counters and wait queues are transparent and can only be accessed through the three ways provided by the semaphore model, namely, the Internet, acquire and release.
Example
Public class SemaphoreDemo {static class Link {} static class ObjPool {final List pool; final Semaphore semaphore; ObjPool (int size, T t) {pool = new Vector (size); for (int I = 0; I
< size; i++) { pool.add(t); } semaphore = new Semaphore(size); } public R exec(Function func) throws Exception { T t = null; semaphore.acquire(); try { System.out.println(Thread.currentThread().getName() + "---------争夺锁--------"); t = pool.remove(0); System.out.println(Thread.currentThread().getName() + " 拿到锁执行"); return func.apply(t); } finally { pool.add(t); semaphore.release(); } } } public static void main(String[] args) { ObjPool objPool = new ObjPool(5, new Link()); for (int i = 0; i < 30; i++) { new Thread(() ->{try {objPool.exec (t-> t.toString ());} catch (Exception e) {}}) .start () Thank you for your reading. The above is the content of "how java uses Semaphore to achieve current limiter". After the study of this article, I believe you have a deeper understanding of how java uses Semaphore to achieve current limiter, 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.