Semaphore w/negative permits available

494 views
Skip to first unread message

tim.russ...@gmail.com

unread,
Feb 17, 2014, 11:16:15 AM2/17/14
to haze...@googlegroups.com
It appears that SemaphoreProxy puts a floor of 0 on the number of available permits. This is obviously necessary for normal acquire operations but I think it should be possible to reduce the number to a negative number if resources become unavailable. Doing so would match the behavior of java.util.concurrent.Semaphore. That class's constructor explicitly allows negative initial permits. Further, calls to reducePermits allow the resulting permit count to be negative.

  • Semaphore

    public Semaphore(int permits)
    Creates a Semaphore with the given number of permits and nonfair fairness setting.
    Parameters:
    permits - the initial number of permits available. This value may be negative, in which case releases must occur before any acquires will be granted.
(my emphasis)




Code example follows:


package test;

import java.util.concurrent.Semaphore;

import com.hazelcast.config.Config;
import com.hazelcast.config.SemaphoreConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ISemaphore;

public class SemaphoreTest {

   
public static void main(String[] args) {
       
new SemaphoreTest().go();
   
}
   
   
public void go() {
       
MyJavaSemaphore myJavaSemaphore = new MyJavaSemaphore(5);
       
System.out.println(myJavaSemaphore.availablePermits());
        myJavaSemaphore
.tryAcquire();
        myJavaSemaphore
.tryAcquire();
        myJavaSemaphore
.tryAcquire();
       
System.out.println(myJavaSemaphore.availablePermits());
        myJavaSemaphore
.reducePermits(4);
       
System.out.println(myJavaSemaphore.availablePermits());
       
       
Config config = new Config();
       
SemaphoreConfig semaphoreConfig = new SemaphoreConfig().setName("default").setInitialPermits(5);
        config
.addSemaphoreConfig(semaphoreConfig);
       
HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
       
ISemaphore myHazelcastSemaphore = instance.getSemaphore("default");
       
System.out.println(myHazelcastSemaphore.availablePermits());
        myHazelcastSemaphore
.tryAcquire();
        myHazelcastSemaphore
.tryAcquire();
        myHazelcastSemaphore
.tryAcquire();
       
System.out.println(myHazelcastSemaphore.availablePermits());
        myHazelcastSemaphore
.reducePermits(4);
       
System.out.println(myHazelcastSemaphore.availablePermits());
   
}
   
   
   
// Only exists to expose reducePermits, which is protected
   
// in the base class.
   
class MyJavaSemaphore extends Semaphore {
       
public MyJavaSemaphore(int permits) {
           
super(permits);
       
}

       
public void reducePermits(int reduction) {
           
super.reducePermits(reduction);
       
}
   
}
}


The output is:

5
2
-2
5
2
0


Does anyone have any thoughts? I would be open to making the change myself and creating a pull request.





Peter Veentjer

unread,
Feb 17, 2014, 2:32:09 PM2/17/14
to haze...@googlegroups.com
Sounds like a viable addition. If you could create a github issue for it and it would be great if you can also do the actual coding.

However we are currently in a feature freeze till 3.2 is released. So this feature will not be added before the 3.3 release.




--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+...@googlegroups.com.
To post to this group, send email to haze...@googlegroups.com.
Visit this group at http://groups.google.com/group/hazelcast.
To view this discussion on the web visit https://groups.google.com/d/msgid/hazelcast/de0cab4d-b39d-4857-bf11-52ed3b8ee4e7%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

openh...@gmail.com

unread,
Feb 20, 2014, 4:51:11 PM2/20/14
to haze...@googlegroups.com
Thanks -- I'll do that.
Reply all
Reply to author
Forward
0 new messages