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 is semaphore Semaphore

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

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what Semaphore is. The quality of the article is high. Therefore, Xiaobian shares it with you as a reference. I hope you will have a certain understanding of relevant knowledge after reading this article.

The essence is the semaphore model. The model diagram is as follows:

The counters and waiting queues are transparent to the outside world and can only be accessed through the three main methods provided.

What are the three main ways?

init()

Used to set the initial value of the counter.

down()

Counter-1. If counter 0 means there are no blocking threads at this point, then only ≤0 requires waking up a waiting thread.

Down() and up() should be paired and used in sequence:

HarmonyOS Technology Community

First call down() to get the lock

After execution, call up() to release the lock

If the semaphore init value is 1, there should not be>0 in concurrent scenarios unless you deliberately use up() first, but this also loses the meaning of semaphore.

Note that these methods are atomic, guaranteed by the implementation of the semaphore model. The semaphore model in JDK is implemented by Semaphore, which ensures that all three methods are atomic operations.

talk is cheap,show me code?

The down() and up() in the semaphore model were originally called P operation and V operation, and the semaphore model was also called PV primitive. Others express them as semWait() and semSignal(), which are called differently but have the same meaning. JUC acquire() and release() correspond to down() and up(), respectively.

How to use semaphores?

Just like a traffic light, you have to check if it's green before you can pass. For example, accumulator, count+=1 operation is a critical section, only one thread is allowed to execute, that is, to ensure mutual exclusion.

Suppose threads t1 and t2 access add() at the same time. When acquire is called at the same time, since acquire is an atomic operation, only one thread (assuming t1) decrements the counter in the semaphore to 0, and t2 decrements the counter to-1:

For t1, the counter value in the semaphore is 0, ≥0, so t1 is not blocked, but continues to execute

For t2, the counter inside the semaphore has a value of-1,

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

Development

Wechat

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

12
Report