📄 semaphore.java
字号:
/**
* File : Semaphore.java
* Author : Fidel Viegas (viegasfh@hotmail.com)
* Date : 23/02/2000
*/
package uk.co.brainycreatures.jpascal.type;
public class Semaphore {
private long value; // value of semaphore
// constructor for binary semaphores
public Semaphore() {
value = 0;
}
// constructor for counting semaphores
public Semaphore(long initial) {
value = initial; // initial value of the semaphore
}
// if the value of the semaphore is
// greater than zero, then its value is decremented
// by one
public synchronized void Wait() {
while (value <= 0) {
try {
wait();
}
catch (InterruptedException e) {
}
}
value--;
}
// if the processes are blocked on the semaphore,
// then unblock one of them
public synchronized void Signal() {
value++;
notify(); // notify the process
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -