Multithreading - ios multithreading (part 1)
Show
gcd, thread, multi thread, processThe most important one which every user expects include execution of demanding tasks in less time, best user experience, no frozen user interfaces, and so on. Now a days every device support multiple CPU cores to achieve this. Multiple cores allow to run multiple processes at the same time. This can be achieved by using multiple thread. What is process?A process is a program that is running on your computer. What is Thread? A thread is sequence of instructions that can be executed by a runtime. What is Multi Thread application?We could say that the application is multithreaded since it uses more than one thread. What is use of Multi Thread? Multithreading is a pattern to increase the performance of an application and scheduling work on multiple threads, more work can be performed in parallel. How can we achieve Multithreading?Your device has multiple CPU cores. To make use of these cores, you will need to use multiple threads. Perform work in parallel is also known as concurrency. Why do We Need Concurrency?
Is there any disadvantage of using MultiThread? when we compare with performance, multithreads are benefit but there are some disadvantages as well.
What is Main Thread? Each process has at least one thread. In iOS, primary thread on which process is started is commonly called as Main thread is used to check current running thread is main thread or notCreating and managing threads Manually:where it is left to the developer to manage all concurrency. Developer need to create and manage the thread. You can Create thread by two ways
Thread.detachNewThreadSelector( 2. Thread can be create by using init() method as follows: If you just initialise the thread then it will not execute the code immediately as above. you need to explicitly call thread.start() method to execute. We can even call .cancel() in order to cancel the execution, perform(Selector, with:, afterDelay: TimeInterval, inModes: [RunLoop.Mode]) to perform action after delay.
To solve these issues we have next way to create the threads using GCD (Grand Central Dispatch): GCD (Grand Central Dispatch):GCD is the abstract away for thread management code and makes working with threads easier and more efficient. It moves all thread creation and management work down to the system level. It offers developers an API that facilitates the scheduling of tasks. Which thread is used to execute a task is handled by Grand Central Dispatch, not the developer and execute them on an appropriate dispatch queue.
GCD manages a collection of dispatch queues. They are usually referred as queues. The work submitted to these dispatch queues is executed on a pool of threads GCD takes care of all thread creation, management and scheduling. It also helps to keep track of total number of threads in your app and it’ll not cause leak.
Once the queue is created, the operating system is the one that manages it and gives time to process on any core of the CPU. Multiple queues are managed accordingly and developer don’t have to deal with management. Queues follows first in first order(FIFO), meaning that the queue that comes first for execution will also finish first.
What is synchronous(sync) or asynchronous(async) task?
How can you achieve synchronous or asynchronous task? This can be achieved by calling sync and async call with global queues.
It means that the thread that initiated that operation will wait for the task to finish before continuing. Task will start in the order they’re added and they always finish in same order The The
Task will start in the order they’re added but they can finish in any order as they can be executed in parallel. Task will run on distinct threads that are managed by dispatch queue. This completes a task in background and can notify you when it complete. no need to wait till task complete. The
The Which one to use?Serial queues take advantage of CPU optimisations and caching, and help reduce context switching. Apple recommends starting with one serial queue per subsystem in your app-for example one for networking, one for file compression, etc Broadly speaking, GCD provides three kinds of queues which are available to you:
Main Queue and Global Queues:- Every application has access to a main queue and several background queues. the main queue is associated with the main thread. What does that mean? Work that is scheduled on the main queue is guaranteed to be executed on the main thread. Grand Central Dispatch also manages a number of concurrent queues that perform work on a pool of background threads. These dispatch queues are also known as global queues. This code will run on main threadThis code executes on global queuesglobal queue is concurrent by default and executes code by Private queues (can be serial or concurrent, you create them):- you can create your own
concurrent sync and async
Parameters:- Label : defines the name of the queue. It helps to identify queue in debug tool like instruments, crash report. It is recommended that we use a reverse DNS naming convention qos (quality of service) : This value determines the priority at which the system schedules tasks for execution. Types are User-interactive,User-initiated, Utility, Background. Higher the priority, higher will be the allocation of resources to that queue.
2.
3.
4.
5.
6.
mapping priority with qos class
Background qos has lowest priority when it is compared with userInteractive qos so userinteractive block finishes execution first then background task. attributes: It include the If the this parameter is nil, a serial queue will be created. two option in attributes1. queue is not active 2. queue is active
autoreleaseFrequency: The frequency with which to autorelease objects created by the blocks that the queue schedules.
Using target: The target queue on which to execute blocks. A dispatch queue’s priority is inherited from its target queue. It has 3 types
Target queue uses:
Advantages of GCD:
Disadvantages of GCD:
Note:
Enjoy your coding. I hope you learnt something from this blog. Please hit the clap button below 👏 to help others find it!. follow me on Medium. Does iOS support multi threading?Concurrency and multithreading are a core part of iOS development. Let's dive into what makes them so powerful, and how we can leverage them in our own Cocoa Touch applications. Concurrency is the notion of multiple things happening at the same time.
What is multithreading in iOS Swift?Multithreading in Swift allows the CPU to produce and run many threads simultaneously. A CPU typically does one operation at a time. However, by using multithreading in Swift, we might be able to let the CPU switch between various tasks so that they can run at the same time.
Which is the best of GCD NSThread or NSOperationQueue?NSOperationQueue works best when tasks are discrete, synchronous and live in the same thread (eg they are more or less atomic), the queue can be used as basic thread pool though in almost any situation.
What is the difference between Operationqueue and GCD?NSOperationQueue is objective C wrapper over GCD . If you are using NSOperation, then you are implicitly using Grand Central Dispatch. That means, the NSOperation API is a higher level abstraction of Grand Central Dispatch which makes NSOperation slightly shower than GCD.
|