Logo

dev-resources.site

for different kinds of informations.

Single and Multi-Threaded Programming with Concurrency and Parallelism - Explain The Differences and Concepts

Published at
8/29/2024
Categories
flutter
dart
programming
mobiledevelopment
Author
a_greynoon
Author
10 person written this
a_greynoon
open
Single and Multi-Threaded Programming with Concurrency and Parallelism - Explain The Differences and Concepts

In the world of programming, you definitely will face the concepts of single and multi-threaded programming, these concepts are closely tied to how a programming language handles concurrent execution and parallelism. In this article, we will explore the concepts and differences between them. But first, we need to understand concurrency and parallelism.

Also... why should we know about this? πŸ€” why would we bother ourselves to learn this? πŸ€”
trust me, I'm cooking something πŸ‘¨πŸ»β€πŸ³πŸ”₯ and everything is connected so be patient and read. πŸ€—

Concurrency Vs Parallelism

Image description

  • Concurrency: refers to the ability of a system to handle multiple tasks at once. However, this doesn't mean that the tasks are literally being executed simultaneously (at the exact same moment). Instead, concurrency allows multiple tasks to make progress during overlapping time periods.

  • Parallelism: occurs when two or more tasks run at the same time (simultaneously). They start, run, and complete in parallel, for example in your PC when your processor has more than one core, it is capable of running multiple threads at the same time. Each processor core can be working on a different task. This is a form of parallelism.

In a single-core processor, for example, it can only execute one instruction at a time. However, the operating system can rapidly switch between tasks, giving each task a small slice of time to execute before moving on to the next task. So, even though the processor is only executing one task at any given moment, it switches between tasks so quickly that it appears as though they are happening concurrently.

In multi-core processors also, for example, true parallelism can occur where different cores handle different tasks simultaneously. However, concurrency still applies because even within a single task, the system may break it down into smaller sub-tasks that are managed concurrently.

Image description

Understanding Single and Multi-Threaded Programming

Image description

Single-Threaded Programming

In single-threaded programming, a program operates on a single thread of execution. This means that the program executes one task at a time in a sequential manner, which means that a program follows the order of operations as defined in the code, executing one operation at a time. But if it encounters a time-consuming task (like for example waiting for a file to load or fetching data from a remote server), it must finish this task before moving on to the next.

This is inherently non-concurrent and non-parallel cause there's only one sequence of operations and in a single-threaded environment, there is no true concurrency because if tasks appear to run simultaneously they are actually being interleaved or managed through time-slicing, in which the opposite of what we said previously "The Concurrency refers to the ability of a system to handle multiple tasks at once", which creates the illusion of concurrency.

But for Parallelism, a Single-threaded environment cannot achieve true parallelism because there’s only one thread doing all the work.

Multi-Threaded Programming

In multi-threaded programming, a program runs multiple threads concurrently. These threads can perform different tasks simultaneously or handle multiple parts of a single task.

A multi-threaded program can split tasks into multiple threads, which can be run either concurrently or in parallel depending on the system's capabilities and the nature of the tasks.

In a multi-threaded environment, concurrency is achieved by allowing multiple threads to make progress on different tasks or parts of the same task. Concurrency doesn't necessarily mean that these tasks are being executed at the same time; it just means that they are being managed in such a way that it seems like they are happening simultaneously.

Parallelism occurs in a multi-threaded environment when multiple threads actually run at the same time on different cores of a multi-core processor. This is true parallel execution, where tasks are processed simultaneously, leading to potentially significant performance improvements.

Image description

Conclusion

Now, we understand the difference between concurrency and parallelism, Concurrency is managing multiple tasks that execute during overlapping periods which is often achieved on single-core processors, while parallelism is about executing multiple tasks at the same time which is possible on multi-core processors.

In single-threaded programming, tasks run sequentially without true concurrency or parallelism, but multi-threaded programming can run concurrently or in parallel improving performance by handling multiple tasks more efficiently.

Next article, we will learn more about these concepts and theories, and how to implement them using Dart programming language, and now I bet you know why 😁😁

Thank you for your time πŸ₯°πŸ₯°

I hope this article has made things clear to you.

mobiledevelopment Article's
30 articles in total
Favicon
MongoDB vs. Couchbase: Comparing Mobile Database Features
Favicon
Migrating a Mobile App from Unity to Flutter
Favicon
Building a Carshare App with React Native
Favicon
How to Deploy Multiple Versions of Your React Native App to TestFlight Using Xcode Cloud
Favicon
Getting Started with Asynchronous Programming in Dart
Favicon
Single and Multi-Threaded Programming with Concurrency and Parallelism - Explain The Differences and Concepts
Favicon
Asynchronous & synchronous Programming In Dart
Favicon
Voice Assistants and Chatbots: Integrating AI-Powered Communication in Apps
Favicon
7 Best Apps Built with Flutter Framework
Favicon
Building a Mobile App with React Native A Step-by-Step Guide
Favicon
Mobile Development
Favicon
Revolutionizing Business with Mobile App Development
Favicon
Common Errors for New Flutter Developers: Tips and Fixes
Favicon
Registering for Apple and Google Developer Accounts
Favicon
Web & Mobile App Development Company- Sapphire Software Solutions
Favicon
Enabling Developer Mode on iOS 17.3.1
Favicon
Kaleidoscope of iOS app architectures
Favicon
Top 50+ Mobile Development Test Cases
Favicon
From Concept to Creation: My Story Behind Snoozle
Favicon
Desvendando um Desafio Comum no React Native: Reduzindo o Tamanho do APK πŸš€
Favicon
Understanding ThemeData in Flutter: Basics and Usage
Favicon
Best Practices for Theme Data: Efficient and Maintainable Ways to Manage Theme Data in Flutter
Favicon
The Pros and Cons of React Native vs. Flutter for Cross-Platform Mobile Development
Favicon
2023 Industry Trends in Mobile Application User Interface
Favicon
What is flutter - It's Pros and Cons
Favicon
What Are The Different Types Of Mobile Applications
Favicon
πŸ“± How To Set Up Your Android Development Environment (React Native & Mac OS)
Favicon
Mastering Mobile Functional Testing: Tips and Tricks for Success
Favicon
How To Set Up a New Project With React & Capacitor.js
Favicon
How to build a mood tracker app in Flutter

Featured ones: