aspose file tools*
The moose likes Threads and Synchronization and the fly likes How can i call my asynchronous task via synchronous method ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How can i call my asynchronous task via synchronous method ?" Watch "How can i call my asynchronous task via synchronous method ?" New topic
Author

How can i call my asynchronous task via synchronous method ?

alp carsikarsi
Ranch Hand

Joined: Dec 27, 2005
Posts: 40
Hello,

I wrote an application which is sending Sip Invite Message to a destination. After some period (10 ms,100 ms,whatever) response message comes from destination. I'm doing some businnes in processResponse.
So this issues are asynchronous!!! By the way i'm using nist library for sip messaging.

Another project team requesting a library from me. They want to use my application by calling synchronously.

What is the simplest way to do this ?
How can i call my asynchronous task via synchronous method ?

Could you share code samples,if you have any ?

Best Regards.





Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2447
    
  28

1)send request
2) poll for response in a loop, sleep for sometime in the loop
3) return response to caller.
alp carsikarsi
Ranch Hand

Joined: Dec 27, 2005
Posts: 40
Thanks jayesh. is this solution works on heavy simultaneus request load ?

Best regards.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

So what exactly is asynchronous? Just use a blocking stream/channel and when all data has arrived you finish up.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2447
    
  28

alp carsikarsi wrote:Thanks jayesh. is this solution works on heavy simultaneus request load ?

Best regards.


Any solution that uses a synchronous call that waits on a background job to complete will have an inherent cost:- The thread that called your synchronous method will wait till the background process is complete. This means that if your app server spawns a thread for each request, there is one more thread for the OS to keep track off, or if the app serves uses a thread pool (as most app servers do), there is one less thread to service other requests. Either way, you will be bottlenecking. This may or may not be a big deal depending on the frequency of requests, and time taken to process each request.

Besides that, if you use the mechanism that I described, there will be an overhead while you continuously poll for completion. It depends on what the cost of each call is. I am not familiar with the library that you are using. If the mechanism that Stephen described is available, it should be less costly, since you will be spending more time in wait
alp carsikarsi
Ranch Hand

Joined: Dec 27, 2005
Posts: 40
In the attachment i sent the flow.


[Thumbnail for a.jpeg]

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

After you send the request, how would you then retrieve the response? Do you need to provide a "callback" to the library?

If so, then you should write an await() signal() notification system between the initial thread and the callback thread.

Note that I wrote this code from the top of my head and I did not compile or test it. Use it only to give you an idea of how to solve the problem.
alp carsikarsi
Ranch Hand

Joined: Dec 27, 2005
Posts: 40
Thanks a lot Stephan van Hulst...

I solved my problem. I'm grateful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can i call my asynchronous task via synchronous method ?