+ Start a Discussion
ArjunmcaArjunmca 

How to call two methods at a time ( synchronously)

Hi,

 

I have two methods each are calling and parsing xml response. But i want those two methods call at a time. Lets say i have two methods. 

 

Method1();

Method2();

 

In the above case first Method1() calls and then Method2() calls. But i have a scenario where i need to call both the methods at a time. 

 

Is there any process to call those methods  Parallely?

 

Thanks.

sfdcfoxsfdcfox
Each transaction only has one thread, so all code has to run synchronously. Even future methods, which run asynchronously, run in their own thread, and can't spawn other future methods.

You would actually mean to say you want to call those two methods asynchronously in the traditional usage of the word. When code is synchronized, that means that only one of two or more functions can execute at once (the first would hold a lock until it completes, then the other code would get a chance once unlocked).

Regardless, in short, you cannot do that natively. Even if there were threads, they would be limited in terms of CPU time and other resources (perhaps even more limited than a regular single-thread system, in order to be fair to other clients).

If you need to process both "at once", you'll have to devise a co-routine mechanism. I don't have enough time tonight to write up a decent example (it's complicated), but it can be done with some effort. You'll still be swapping back and forth between the methods, so they'll have to be serializable and deserializable, as well as reentrant and cooperatively multi-tasking. It's not complex, but the code would be quite significant.
ArjunmcaArjunmca

Hi Thanks for quick reply.

 

I have two webservices, and i have written two methods to parse the response of webservices

 

Hence two methods are calling one after another, first one taking 3 sec time and second method taking 5 sec time.

So the user has to wait 8 secs to see the out put. So if i make asynchronous calls to those methods, then i can save 3 sec time right.

 

Could you please share the code when you have some free time.

 

Thanks.

sfdcfoxsfdcfox
Since they'd be cooperatively multitasking, they would still be subject to running synchronously in terms of each callout, so there's no way in salesforce.com, right now, to reduce that total time directly in Apex Code.

If you are using Visualforce, however, you can run them in parallel by using JavaScript. Basically, you can do this:

MyController.firstMethod(data,firstMethodResponse)
MyController.secondMethod(data,secondMethodResponse)

In each callback method (the response methods), you would check to see if you have all of the data; if not, store the results in the page until the other results come back, then do processing. You could also call an actionFunction after both methods return their data in order to update the view state.

See:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_RemoteAction.htm