aspose file tools*
The moose likes Threads and Synchronization and the fly likes Having a time gap between 2 async method calls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Having a time gap between 2 async method calls" Watch "Having a time gap between 2 async method calls" New topic
Author

Having a time gap between 2 async method calls

Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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


Thanks and Regards
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

I think you can make thread to sleep using Thread.currentThread().sleep(2000);
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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

Joined: Apr 14, 2004
Posts: 10336

Describe what you mean be "when needed". Wrapping the call in some conditional logic is easy.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

As Paul Sturrock said use boolean varialbe as flag
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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()


Steve
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having a time gap between 2 async method calls