Java信号量最大值?
有没有办法知道信号量对象在其“生命周期”中拥有的最大许可数量是多少? 我们将其初始化如下:Java信号量最大值?,java,semaphore,Java,Semaphore,有没有办法知道信号量对象在其“生命周期”中拥有的最大许可数量是多少? 我们将其初始化如下: Semaphore sem = new Semaphore(n); 有时我们获得,有时我们释放我们所获得的。但在某些情况下,为了增加许可证数量,我们需要释放比我们获得的更多的许可证。有没有办法知道此信号量中的最大许可数量?构造函数定义为。int的最大值是231-1=2147483647,因此这是您的答案。构造函数定义为。int的最大值是231-1=2147483647,因此这是您的答案。信号量本身在其生
Semaphore sem = new Semaphore(n);
有时我们获得,有时我们释放我们所获得的。但在某些情况下,为了增加许可证数量,我们需要释放比我们获得的更多的许可证。有没有办法知道此信号量中的最大许可数量?构造函数定义为。int的最大值是231-1=2147483647,因此这是您的答案。构造函数定义为。int的最大值是231-1=2147483647,因此这是您的答案。信号量本身在其生命周期内不跟踪最大值。在其周围实现一个Semphore包装器来跟踪最大值可能会很棘手。下面是此类实施的快速草案:
public final class MySemaphore {
private final Semaphore semaphore;
private final AtomicReference<MaxCounter> maxCounter = new AtomicReference<>();
public MySemaphore(int initialAvailable) {
this.semaphore = new Semaphore(initialAvailable);
maxCounter.set(new MaxCounter(initialAvailable, initialAvailable));
}
private static final class MaxCounter {
private final int value;
private final int max;
public MaxCounter(int value, int max) {
this.value = value;
this.max = max;
}
public MaxCounter increment() {
return new MaxCounter(value + 1, Math.max(value + 1, max));
}
public MaxCounter decrement() {
return new MaxCounter(value - 1, max);
}
public int getValue() {
return value;
}
public int getMax() {
return max;
}
}
public void acquire() throws InterruptedException {
semaphore.acquire();
for (;;) {
MaxCounter current = maxCounter.get();
if (maxCounter.compareAndSet(current, current.decrement())) {
return;
}
}
}
public void release() {
for (;;) {
MaxCounter current = maxCounter.get();
if (maxCounter.compareAndSet(current, current.increment())) {
break;
}
}
semaphore.release();
}
public int availablePermits() {
return maxCounter.get().getValue();
}
public int getMaximumEverAvailable() {
return maxCounter.get().getMax();
}
}
公共最终类MySemaphore{
专用最终信号量;
private final AtomicReference maxCounter=新的AtomicReference();
公共MySemaphore(int initialAvailable){
this.semaphore=新信号量(initialAvailable);
set(新的maxCounter(initialAvailable,initialAvailable));
}
私有静态最终类MaxCounter{
私有最终整数值;
私人最终整数最大值;
公共最大计数器(int值,int max){
这个值=值;
this.max=max;
}
公共MaxCounter增量(){
返回新的MaxCounter(值+1,数学最大值(值+1,最大值));
}
公共最大计数器减量(){
返回新的最大计数器(值-1,最大值);
}
public int getValue(){
返回值;
}
公共int getMax(){
返回最大值;
}
}
public void acquire()引发InterruptedException{
semaphore.acquire();
对于(;;){
MaxCounter电流=MaxCounter.get();
if(maxCounter.compareAndSet(current,current.decrement())){
返回;
}
}
}
公开无效释放(){
对于(;;){
MaxCounter电流=MaxCounter.get();
if(maxCounter.compareAndSet(current,current.increment())){
打破
}
}
semaphore.release();
}
公共int可用许可证(){
返回maxCounter.get().getValue();
}
public int getMaximumEverAvailable(){
返回maxCounter.get().getMax();
}
}
MaxCounter可能与内部使用的信号量不完全同步。内部信号量可能会获得一个释放/获取,该释放/获取从外部角度作为获取/释放进行处理。对于MySemaphore的每个客户机,尽管行为是一致的。i、 e.availablePermits()
将永远不会返回高于GetMaximumeAvailable()
免责声明:未测试的代码*信号量本身在其生命周期内不会跟踪最大值。在其周围实现一个Semphore包装器来跟踪最大值可能会很棘手。下面是此类实施的快速草案:
public final class MySemaphore {
private final Semaphore semaphore;
private final AtomicReference<MaxCounter> maxCounter = new AtomicReference<>();
public MySemaphore(int initialAvailable) {
this.semaphore = new Semaphore(initialAvailable);
maxCounter.set(new MaxCounter(initialAvailable, initialAvailable));
}
private static final class MaxCounter {
private final int value;
private final int max;
public MaxCounter(int value, int max) {
this.value = value;
this.max = max;
}
public MaxCounter increment() {
return new MaxCounter(value + 1, Math.max(value + 1, max));
}
public MaxCounter decrement() {
return new MaxCounter(value - 1, max);
}
public int getValue() {
return value;
}
public int getMax() {
return max;
}
}
public void acquire() throws InterruptedException {
semaphore.acquire();
for (;;) {
MaxCounter current = maxCounter.get();
if (maxCounter.compareAndSet(current, current.decrement())) {
return;
}
}
}
public void release() {
for (;;) {
MaxCounter current = maxCounter.get();
if (maxCounter.compareAndSet(current, current.increment())) {
break;
}
}
semaphore.release();
}
public int availablePermits() {
return maxCounter.get().getValue();
}
public int getMaximumEverAvailable() {
return maxCounter.get().getMax();
}
}
公共最终类MySemaphore{
专用最终信号量;
private final AtomicReference maxCounter=新的AtomicReference();
公共MySemaphore(int initialAvailable){
this.semaphore=新信号量(initialAvailable);
set(新的maxCounter(initialAvailable,initialAvailable));
}
私有静态最终类MaxCounter{
私有最终整数值;
私人最终整数最大值;
公共最大计数器(int值,int max){
这个值=值;
this.max=max;
}
公共MaxCounter增量(){
返回新的MaxCounter(值+1,数学最大值(值+1,最大值));
}
公共最大计数器减量(){
返回新的最大计数器(值-1,最大值);
}
public int getValue(){
返回值;
}
公共int getMax(){
返回最大值;
}
}
public void acquire()引发InterruptedException{
semaphore.acquire();
对于(;;){
MaxCounter电流=MaxCounter.get();
if(maxCounter.compareAndSet(current,current.decrement())){
返回;
}
}
}
公开无效释放(){
对于(;;){
MaxCounter电流=MaxCounter.get();
if(maxCounter.compareAndSet(current,current.increment())){
打破
}
}
semaphore.release();
}
公共int可用许可证(){
返回maxCounter.get().getValue();
}
public int getMaximumEverAvailable(){
返回maxCounter.get().getMax();
}
}
MaxCounter可能与内部使用的信号量不完全同步。内部信号量可能会获得一个释放/获取,该释放/获取从外部角度作为获取/释放进行处理。对于MySemaphore的每个客户机,尽管行为是一致的。i、 e.availablePermits()
将永远不会返回高于GetMaximumeAvailable()
免责声明:代码未针对32位体系结构进行测试*。到目前为止,我知道每次定义为4字节值时都是一个int。但我现在要检查一下。哈哈,不,那不是我的问题。我的问题是我在这个特定信号量中输入的最大值。@我不能保留另一个单独跟踪它的变量吗?在Java中,
int
总是一个32位有符号的数字。而long
始终是64位有符号整数。即使在64位JVM上,引用通常也是32位的。对于32位体系结构,到目前为止,我知道每次定义为4字节值时都是int。但我现在要检查一下。哈哈,不,那不是我的问题。我的问题是我在这个特定信号量中输入的最大值。@我不能保留另一个单独跟踪它的变量?在