信号量简单的样本 [英] Semaphore simple sample
本文介绍了信号量简单的样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以分享使用信号量的简单示例吗?
如果有可能解决没有信号量的任务,然后用信号量来理解它的主要想法。
Can anyone share simple sample of using Semaphore? If it's possible a sample of solving a task without semaphore and then with semaphore to understand main idea of it.
推荐答案
这是一个简单的信号量实现:
Here is a simple Semaphore implementation:
public class Semaphore {
private boolean signal = false;
public synchronized void take() {
this.signal = true;
this.notify();
}
public synchronized void release() throws InterruptedException{
while(!this.signal) wait();
this.signal = false;
}
}
take()
方法发送一个内部存储在信号量中的信号。 release()
方法等待信号。收到信号标志后再次清除,退出 release()
方法。
The take()
method sends a signal which is stored internally in the Semaphore. The release()
method waits for a signal. When received the signal flag is cleared again, and the release()
method exited.
这篇关于信号量简单的样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文