Conversation


Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0


advertisement

Synchronous and Asynchronous Programming in Dart

synch and asynch in dart


Synchronous Programming

Consider a real-world scenario. You are speaking on the phone with someone. At the same instant, you cannot make another call. The next call can be made only after disconnecting from the present call. The present task is 'blocking' the next task from happening until the present task is completed.

This concept can be carried forward to understand synchronous programming in applications. The execution of one process depends on another process. The system cannot execute any other process unless the first process has been completed.

To simply explain this, a synchronous process waits for an event or operation to be completed before it starts to execute another event or process.

The biggest disadvantage of such a process is that it blocks the process or code until the previous process has been executed completely. In a Web Server scenario, the request coming from another user will be blocked until the previous user request is executed.

import 'dart:io';

void main() {
  print("Enter Your Name: ");
  String name = stdin.readLineSync();
  print("Your name is $name");
}



Asynchronous programming

An asynchronous method executes in a thread that is not the main application thread.

To understand this, let us consider an example.

When you are speaking on the phone, you can send a message to a person who is trying to reach you. It is not necessary to disconnect the present call to message.

Similarly, in Asynchronous programming, the execution of one process does not depend on another process.

When something is executed asynchronously, the user can move on to another task before it is completed or executed. Asynchronous programming fixes the chain of events in a programming cycle.

import 'dart:io';
import 'dart:async';

void main() {
  File file = File(Directory.current.path + "/names.txt");
  Future<String> f = file.readAsString();

  f.then((data) {
    print(data);
    print("main ends here");
  });
}



Advantages and Disadvantages of Synchronous and Asynchronous Programming

Synchronous and Asynchronous programming are categorized into two different programming styles, with their advantages and disadvantages. It is important to understand the differences between the two styles to evaluate the programming requirements and select the best solution for the software activity as no programming style is better.

Advantages of synchronous programming

  • Synchronous programming is supported in all programming languages.
  • The programs are easier to write.
  • Synchronous programs are reliable as errors can be detected and handled before they cause significant damage.

Disadvantages of synchronous programming

  • Programming loading time is slow.
  • When a thread is locked, the threads that are in the queue get blocked as well.
  • It requires a significant number of resources.
  • It requires more threads to handle more requests.

Advantages of asynchronous programming

  • In asynchronous programming, the application can continue executing while an asynchronous function performs its task.
  • Asynchronous programming loads the complete program at once.
  • Asynchronous programming decreases page load delays.
  • The call-back functions help to execute the next line of code if any error occurs while executing an asynchronous program.
  • Async and await keywords have well-known syntax structures in programming languages that enable asynchronous functions to work with promises comfortably.

Disadvantages of asynchronous programming

  • Asynchronous programming is complex to write.
  • Developers should know call-back and recursive functions.
  • Rendering a page takes time in asynchronous programming.
  • If there are too many asynchronous requests, it can overload the server, and program execution will be slow.


Dart Synchronous Programming Asynchronous Programming Difference sync and async

advertisement