Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 184.108.40.206 release ready!
First, we will see what is a queue, a queue is a block of code that is managed by the operating system to execute it synchronously or asynchronously either on the main thread or background thread. A queue follows the “FIFO” order. The queue can be either Serial or Concurrent.
Serial:- In the serial, the work item will not start the execution until the previous one is finished.
Concurrent:- In this, the execution occurs parallelly.
First, we create a dispatch queue.
For label a reverse-DNS naming style (com.example.myqueue) is recommended. Let create a method and on which we create our Dispatch Queue.
The first For loop will execute on the background thread and the last one will execute on the main thread. If we call the myFirstQueue method then it prints “1 2 3 4 5 6 7 8 9 10 20 21 22 23 24 25 26 27 28 29 30”. In the above method, we have used synchronous execution, so unless the queue code is not finished it will not execute the main thread code.
Now we will execute the code asynchronously and see what will happen.
We will see that the code on the main thread and the code in the dispatch queue will execute parallelly.
To determine the priorities of the task we used the QoS class property. It is done so that system will know which task has higher priority. So that the execution has to be done accordingly. Although the task runs on the main thread has the highest priority since it makes UI responsive.
The below-listed QoS case is set on the order of its priority. The first one has the highest priority while the last one has the lowest. You can find the documentation from here.
Now we see some example of QoS
In the above example, the priority of “firstQueue” is higher than “secondQueue”. When we call the exampleQoS method then we can see the output from the below image.
We can see clearly that the priority of “firstQueue” is higher than “secondQueue” so it executes faster.
So, far we have seen that our queue is serial, which means if we assign more than one task to a single queue then it is executed one after another. Now we will see how they will run together. In another word, we will see a concurrent queue.
Let first see one example
From the above screenshot, we can see when we call the “exampleQueue” method, then we can see all the tasks execute in a serial manner.
Now we will add the extra argument “attributes” with value concurrent, our queue is now concurrent, when attributes are not present then it is serial. Let see its example.
When we run the above method “concurrentQueueExample” then we can see that the task will execute simultaneously. Also, change in QoS will also affect the task execution. The attributes also take another value “initiallyInactive“. When we pass “initiallyInactive” the task does not start automatically, it needs to trigger to start the execution.
Let see an example.
When the above method is called then it crashes. We need to activate it first. For this, we will use activate() method.
Note:- We have not passed the concurrent value in attributes, so it will execute in a serial manner. To make it concurrent we will pass both the “initiallyInactive” and “concurrent” in the attributes.
Now, we see how we delay the execution of work. Let see by an example
We can also use DispatchTimeInterval for providing the delay time. We can use seconds, microseconds, nanoseconds, and milliseconds. Let take an example of it.
So, Far we are creating a custom queue, but the system creates a collection of background queues which is known as global queues. If we don’t want to use a custom queue then we can use global queues.
We can also specify the quality of service that we want to use.
Now we will see how to access the main queue. The main purpose of the main queue is for UI update, All the UI updating parts are done on the main thread. And the task which required more time is done on the background thread.
When we don’t want to use the code block then at the time we used DispatchWorkItem. A contain codes of DispatchWorkItem can be executed on a background thread or main thread.
Let see an example of it.
You can also check other blogs from here. if you have any issue or suggestion you can leave your query/suggestion in the comment section.
You can check more information from Appcoda
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.