Calling two or more API with certain time gap

V

Vallabha

Hello All,

I have a multi-threaded application. This application makes use of
certain APIs from a third party library. One constraint using these
APIs is that no two APIs should be called with in one minute. For
example, if the three APIs are Foo1(), Foo2() and Foo3() then Foo1 and
Foo2 can not be called within one minute.

One thing I can do is before calling any of these APIs put a sleep for
1 minute and then call. Is there a better way than putting sleep
statement everywhere?

Any thoughts on this would be of great help.

Thanks.
 
T

Tom Anderson

I have a multi-threaded application. This application makes use of
certain APIs from a third party library. One constraint using these APIs
is that no two APIs should be called with in one minute. For example, if
the three APIs are Foo1(), Foo2() and Foo3() then Foo1 and Foo2 can not
be called within one minute.

One thing I can do is before calling any of these APIs put a sleep for 1
minute and then call. Is there a better way than putting sleep statement
everywhere?

Any thoughts on this would be of great help.

public class ThrottleLock {
private final long interval ;
private long lastAcquired ;

public ThrottleLock(long interval) {
this.interval = interval ;
this.lastAcquired = System.currentTimeMillis() ;
}
public synchronized void acquire() throws InterruptedException {
long now ;
while (((now = System.currentTimeMillis()) - lastAcquired) < interval)
Thread.sleep(interval - (now - lastAcquired)) ;
lastAcquired = now ;
}
}

Set up a ThrottleLock with interval = 1 minute = 60*1000 ms. Have your
code call ThrottleLock.acquire() before calling any of the API functions.

This code doesn't guarantee fairness for threads waiting on the lock (i
think). You could rewrite it to use a lock from java.util.concurrent if
you needed that.

It would be a good idea to add a way to cancel the lock, such that all
then-waiting or -sleeping threads come back immediately with an
InterruptedException. You'd need to do this to enable a timely orderly
shutdown.

If your constraint is more precisely that no API function should be called
within a minute of another one *returning*, rather than being called,
you'll need to add a release() method, to call after the return, and some
slightly more complicated lock logic using wait/notify.

tom
 
G

Gordon Beaton

Hello All,

I have a multi-threaded application. This application makes use of
certain APIs from a third party library. One constraint using these
APIs is that no two APIs should be called with in one minute. For
example, if the three APIs are Foo1(), Foo2() and Foo3() then Foo1 and
Foo2 can not be called within one minute.

One thing I can do is before calling any of these APIs put a sleep for
1 minute and then call. Is there a better way than putting sleep
statement everywhere?

Any thoughts on this would be of great help.

This sounds construed, much like homework often is.

Here's a hint anyway: leaky bucket.

/gordon

--
 
G

Gordon Beaton

"Construed" as in "translated" or "interpreted"? Translated or interpreted
from what to what, or as what?

I'm sorry, but I do not understand your comment.

Sorry, contrived. (Was thinking of the Swedish word "konstruerat"...)

/gordon

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top