Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Synchronousand Asynchronous Programming - Copy

Uploaded by

23saheeljoomun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Synchronousand Asynchronous Programming - Copy

Uploaded by

23saheeljoomun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SYNCHRONOUS PROGRAMMING

• It is a programming where operations take place sequentially.

• Programs are executed in a sequential manner. Thus, one operation must be fully completed before the
next operation can begin.

• Tasks are executed in order, one after the other, and it cannot be interrupted until finished.

• Waits for a function to complete before moving on to the next line of code.

• Must wait for the previous tasks to finish before it begins.

• Works best when tasks are short and straightforward enough so that there is no need to execute
multiple tasks simultaneously.

ADVANTAGES DISADVANTAGES

Easy to understand Slow and unresponsive


Simple and straightforward Not suitable for all tasks
Default way because tasks occur in a Not the best option for long-running tasks or
predictable order. heavy processing

Requires minimal setup. Slower program execution time

HOW DOES SYNCHRONOUS PROGRAMMING WORK?


It works by creating a linear sequence of operations. One must fully be executed before the other is
executed.
Each line is executed one after the other.
ASYNCHRONOUS PROGRAMMING

• It is a programming where code execution is separated into multiple tasks that can run independently
without waiting for one task to finish so that another can begin.

• It is possible by writing code split into smaller, independent tasks and running each task in its thread.

• Tasks can be executed in any order or even simultaneously.

• By creating threads for each task, it makes it possible to do many tasks simultaneously by allowing them
to run in parallel.

• Once all the tasks are finished, they can synchronize to return a result.

• Works best when tasks are long, complex and require multiple supplies.

• Tasks are not dependent on previous tasks.

• For example, when a web application sends an asynchronous request to the server and multiple requests
can be executed concurrently. The application can continue to run while waiting for the response.

HOW DOES ASYNCHRONOUS PROGRAMMING WORK?


It relies on non-blocking I/O, which allows a program to initiate an operation without waiting for it to be
completed. The program can continue running and the operation is executed in the background. Some
of its core concepts are:

1. Callback functions
A callback function is passed as an argument to an asynchronous operation. When the operation is
complete, the callback function is called, often with the result if the operation is a parameter.
It allows a program to perform other tasks while waiting as to not block and wait for the operation to
finish.

2. Promises
Promises are another important concept in asynchronous programming. A promise is an object that
represents the eventual result of an operation. When a promise is created, it is pending and as the
operation progresses the promise is either fulfilled with a result or rejected with error. It is a bit like
callback functions.

3. Async/await
This is a newer syntax for asynchronous programming, that is built on top of promises. It provides a
simplified way to write asynchronous code that reads more like synchronous code. It returns promises
and can be used to wait for the resolution of a promise without blocking the program.
ADVANTAGES DISADVANTAGES

Requires more effort and expertise as careful


Faster app performance and responsive user
planning and proper implementation are
experience.
necessary.
Better error management, it is easier to manage It can be challenging and time- consuming to
errors and failures. identify and fix bugs.
Better use of system resources and improved
overall efficiency.

Synchronous or Asynchronous programming?

The choice depends on the goal of the program. If we need to complete tasks in a specific order and
time are not important, then synchronous programming is best.

However, if you need to execute multiple tasks simultaneously and need results of each task to continue
the program, the asynchronous programming is better.

CAMELCASE EXERCISE:

The camelCaseConverter.java file:


The main.java file:

Here is the output screen:

You might also like