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

How to parse Semaphore semaphores

2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces you how to analyze Semaphore semaphore, the content is very detailed, interested friends can refer to it, I hope it can help you.

A few days ago, some netizens in the group asked me about semaphore and thread pool. I answered some mistakes about semaphore. Other netizens in the group made corrections. Today, due to time constraints, I simply said semaphore and thread pool.

Since jdk 5.0, the official implementation of Semaphore has been provided in the java.util.concurrent package, so you don't have to implement Semaphore yourself. However, it is necessary to become familiar with how Semaphore is used and the principles behind it. A Semaphore is a thread-synchronization structure used to pass signals between threads to avoid signal loss or to protect a critical area like a lock.

Semaphores and thread pools are used in a wide range of scenarios, the simplest of which is that Hystirx provides two modes of execution logic: semaphores and thread pools. Therefore, for this video of JUC, it is still necessary for everyone to take a look.

Semaphore can control how many times a resource can be accessed simultaneously, acquiring a grant by acquire(), waiting if not, and releasing (). For example, under Windows, you can set the maximum number of client accesses to shared files.

Semaphore works like a toilet with five pits. If 10 people have to go to the toilet, how many people can go to the toilet at the same time? At the same time, only 5 people can occupy it. When any one of the 5 people gives way, one of the other 5 people waiting can occupy it. The other five people waiting could be given priority at random or in first-come, first-served order, depending on the parameters passed in when constructing the Semaphore object. Semaphore objects of a single semaphore can perform the function of mutex locks, and one thread can acquire the lock and another thread can release the lock. This can be applied to some cases of deadlock recovery.

Semaphore maintains the current number of accesses and provides synchronization mechanisms to control the number of simultaneous accesses. Linked lists can hold "infinite" nodes in data structures, and Semaphore can implement linked lists of finite size. In addition, Reentrant Lock can also implement this function, but the implementation is more complicated.

Acquire() Here I define 10 people to go to the toilet, but only 5 pits, each person consumes random time, directly run the above code, you can see that at the beginning of the five people, and later one after another someone into, someone out. But there must be no more than five in use.

acquire()

If no permissions are available, the current thread is disabled for threading purposes and put to sleep until either: some other thread calls the semaphore's release() method and the current thread is the next thread to be granted permission; or some other thread interrupts the current thread. Gets a grant from this semaphore, blocks the thread until a grant is provided, or the thread is interrupted. Get a license (if one is provided) and return immediately, subtracting 1 from the number of licenses available.

If the current thread: has its interrupted state set to on by this method; or is interrupted while waiting for permission. An InterruptedException is thrown and the interrupted state of the current thread is cleared.

release()

Releases a grant and returns it to the semaphore. Releasing a license increases the number of licenses available by 1. If any thread tries to acquire a license, select a thread and give it the license you just released. The thread is then enabled (or re-enabled) for threading purposes.

Threads that do not require permission release must acquire permission by calling acquire(). The correct usage of semaphores is established through programming conventions in the application.

Two other less common methods, public int availablePermits(), see what semaphores are available now. public int drainPermits(), this method returns the total number of permissions and sets the permissions to 0. public Semaphore(int permits) where permits is the number of threads allowed to run simultaneously.

About how to parse Semaphore semaphore will be shared here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

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

Servers

Wechat

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

12
Report