This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having a time gap between 2 async method calls

 
Satya Maheshwari
Ranch Hand
Posts: 368
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

What is the best possible way to ensure a minimum time gap between 2 asynchronous method calls. For e.g. if I have 2 methods methodA() and methodB() and there are called asynchronously, how can I ensure that there is a minimum time gap between the calls i.e. the 2nd call must block till that time gap has expired and then only proceed. I have a few solutions in my head but I am looking for a good one.

Regards
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can make thread to sleep using Thread.currentThread().sleep(2000);
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Malatesh Karabisti wrote:I think you can make thread to sleep using Thread.currentThread().sleep(2000);

Ok, but I want to sleep only when needed.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Describe what you mean be "when needed". Wrapping the call in some conditional logic is easy.
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Paul Sturrock said use boolean varialbe as flag
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:Describe what you mean be "when needed". Wrapping the call in some conditional logic is easy.


Let's say there are 2 methods m1() and m2().
The time spacing between m1() and m2() must never be less that say T
So let say m1() was called at some time instant t1.
And then m2() is called at some time instant t2.

Now if t2-t1 < T then block for time t2-t1 and then proceed with m2()
if t2-t1 >= T then proceed with m2()

Also one thing to note is that calls to m1() and m2() are always made in pairs and there could be several such pairs happening simultaneously.



 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps use a Semaphore, initially with no permissions. m2()'s first job is to take a permission from the Semaphore (and therefore waits till it is ready). m1()'s first job is to start a TimerTask delayed for time T, which gives a permission back to the Semaphore.

See:
java.util.Timer#schedule(task, delay)
java.util.concurrent.Semaphore#acquire()
java.util.concurrent.Semaphore#release()
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:Perhaps use a Semaphore, initially with no permissions. m2()'s first job is to take a permission from the Semaphore (and therefore waits till it is ready). m1()'s first job is to start a TimerTask delayed for time T, which gives a permission back to the Semaphore.

See:
java.util.Timer#schedule(task, delay)
java.util.concurrent.Semaphore#acquire()
java.util.concurrent.Semaphore#release()


Thanks Steve. I was thinking on similar lines though had thought of using Lock instead of semaphore.
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I think the Semaphore is a bit better than Lock in this case. For Lock/Condition m2() must be waiting before the timeout occurs. If not, then the timeout can signal the Condition before m2() awaits it and the signal could be lost making m2() wait forever. There are workarounds of course, but the Semaphore is nice and easy. If the timeout occurs before m2() gets to acquire(), then acquire() returns immediately.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic