Logo

dev-resources.site

for different kinds of informations.

Achieving multi-threading by creating threads manually in Swift

Published at
7/9/2024
Categories
swift
threading
multithreading
Author
vinaykumar0339
Categories
3 categories in total
swift
open
threading
open
multithreading
open
Author
14 person written this
vinaykumar0339
open
Achieving multi-threading by creating threads manually in Swift

What is Threading?

Threading is a fundamental concept in programming that allows for the concurrent execution of code. By using multiple threads, you can perform multiple tasks simultaneously, improving the efficiency and performance of your application.

Why Use Threading?

  • Improved Performance: Execute multiple operations in parallel.
  • Responsiveness: Keep your application responsive by performing long-running tasks in the background.
  • Resource Utilization: Make better use of system resources by distributing workloads.

Thread API in Swift

Thread will be available in the Foundation framework

import Foundation

class CustomThread {
    func createThreadUsingObjSelector() {
        let thread = Thread(target: self, selector: #selector(threadExecutor), object: nil)
        thread.start()
    }

    @objc private func threadExecutor() {
        print("Executing threadExecutor \(Thread.current)")
    }

    func createThreadUsingTrailingClosures() {
        let thread = Thread {
            print("Executing createThreadUsingTrailingClosures \(Thread.current)")
        }
        thread.start()
    }
}

let customThread = CustomThread()
customThread.createThreadUsingObjSelector()
customThread.createThreadUsingTrailingClosures()
Enter fullscreen mode Exit fullscreen mode

Pros of using Manual Threads

  1. Fine-Grained Control: You have complete control over thread creation, management, and execution (like start, cancel etc)
  2. Customization: You can customize thread behaviour to suit specific needs.

Crons of using Manual Threads

  1. Resource Intensive: Creating and managing many threads can be resource-intensive and may degrade performance if not managed properly.
  2. Scalability: Manual threading may not scale well with the increasing complexity of applications.
  3. More Power comes with higher responsibility.
  4. Improper management may cause memory leaks in the app.
  5. Managing Order of Execution.

Conclusion

Threading is a powerful tool for improving the performance and responsiveness of your applications. While manual threading provides fine-grained control, it comes with complexity and potential pitfalls. By understanding the pros and cons, and following best practices, you can effectively utilize threading in your Swift applications.

<-- Swift Concurrency                                                                     -->Introduction to GCD

threading Article's
30 articles in total
Favicon
Concorrência e paralelismo em Python
Favicon
Navigating Concurrency for Large-Scale Systems
Favicon
Common Java Developer Interview Questions and Answers on multithreading, garbage collection, thread pools, and synchronization
Favicon
Real-time plotting with pyplot
Favicon
A Quick Guide to the Python threading Module with Examples
Favicon
Understanding Threading and Multiprocessing in Python: A Comprehensive Guide
Favicon
I Asked Copilot to Explain Threading in Python to a Dog
Favicon
Introduction to GCD (Grand Central Dispatch)
Favicon
Achieving multi-threading by creating threads manually in Swift
Favicon
Swift Concurrency
Favicon
Python Multithreading: Unlocking Concurrency for Better Performance
Favicon
Choosing the best asynchronous library in Python
Favicon
Two Lines of Code Eluded me for Several Months
Favicon
A Comprehensive Guide to Python Threading: Advanced Concepts and Best Practices
Favicon
Thread synchronisation
Favicon
Rust Learning Note: Multithreading
Favicon
Async vs Threading vs Multiprocessing in Python
Favicon
02.Android Background Task
Favicon
How to handle threads with multiple gunicorn workers to get consistent result
Favicon
Tasks, BackgroundWorkers, and Threads
Favicon
Understanding Task.WhenAll in C#
Favicon
Producer/consumer pipelines with System.Threading.Channels
Favicon
How to auto-refresh Realm inside Android WorkManager
Favicon
Understanding Task in .Net
Favicon
Como resolvemos um bug que afetava 3000 usuários e nos custaria milhões por ano
Favicon
Java Thread Programming (Part 1)
Favicon
So, you want to launch several threads in Python and something does not work?
Favicon
Higher level threading in C++
Favicon
Solve the scenario - using Thread synchronization in Dotnet - CountDownEvent
Favicon
Что в процессе тебе моем?

Featured ones: