Eddie,
Thanks for the great suggestion. I've implemented your idea as follows:
// Define a 'class'
function Semaphore(name) {
this.name = name;
this.semaphore = java.lang.System.getProperties().get(
this.name) ||
new java.util.concurrent.Semaphore(1);
java.lang.System.getProperties.put(
this.name, this.semaphore);
this.isAcquired = function(seconds) {
return this.semaphore.tryAcquire(seconds, java.util.concurrent.TimeUnit.SECONDS);
}
this.release() {
this.semaphore.release();
}
}
// Two ways to use
var mySemaphore = new Semaphore('some-name-here');
if (!mySemaphore.isAcquired(60)) {
task.logmsg("Semaphore acquisition failed after 60 seconds");
task.shutdown();
}
// or
while(!mySemaphore.isAcquired(0)) {
task.sleep(500 + Math.random()*500); // As in your example