Synchronous and Asynchronous calls using Apex in Salesforce

Apex can be executed synchronously or asynchronously.

Synchronous:

In a Synchronous call, the thread will wait until it completes its tasks before proceeding to next. In a Synchronous call, the code runs in single thread.

Example:

Trigger
Controller Extension
Custom Controller

Asynchronous:

In a Asynchronous call, the thread will not wait until it completes its tasks before proceeding to next. Instead it proceeds to next leaving it run in separate thread. In a Asynchronous call, the code runs in multiple threads which helps to do many tasks as background jobs.

Example:

Batch
@future Annotation

Cheers!!!

2 comments:


  1. can we call synchronous method from an asynchronous method? if yes, what are the issues we will get?

    ReplyDelete
    Replies
    1. No issues in calling synchronous methods from asynchronous methods. They will run in the same thread.
      You cannot call Asynchronous methods from another asynchronous method. You will get an exception in this case.

      Delete