public Semaphore(int permits)
Semaphore with the given number of permits and nonfair fairness setting.permits - the initial number of permits available. This value may be negative, in which case releases must occur before any acquires will be granted.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);
}
}
}
5
2
-2
5
2
0
--
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.